This question already has answers here:
How to replace all occurrences of two substrings with str_replace()?
(3 answers)
Closed 5 years ago.
Say I want to erase any quote from this string, and then replace spaces with '&', in php. I could easily do with 2 consecutive preg_replace or the like, but how to do it in only 1 passage?
" columns="4" link="file" ids="280,281,282,283,284,285,286,287,288""
to:
columns=4&link=file&ids=280,281,282,283,284,285,286,287,288
You don't need regex for that. str_replace() should work fine:
$string = 'columns="4" link="file" ids="280,281,282,283,284,285,286,287,288"';
$replaced = str_replace([' ', '"'], ['&', ''], $string);
Demo: https://3v4l.org/gHHMp
Related
This question already has answers here:
php explode at capital letters?
(4 answers)
Closed 1 year ago.
I have a string that has words without any spaces between them. How do I add a space between each word? Example: my string has combined words FinancialTimes and I would like it to show as Financial Times
<?php $word = 'FinancialTimes';?>
You could use preg_replace with a regex option:
$word = "FinancialTimes";
$output = preg_replace("/(?<=[a-z])(?=[A-Z])/", " ", $word);
echo $output; // Financial Times
This question already has answers here:
Remove all special characters from a string [duplicate]
(3 answers)
Closed 5 years ago.
I want to replace space with "|" and i want only numbers and alphabets and star(*) in the sting
And i have tried str_replace() funtion but it replace the space with | , but i want to validate
echo $text= str_replace(' ','|', "This is some $123 Money");
output
This|is|some|$123|Money
what i expect is
This|is|some|123|Money
I don't want any other special characters in my output
Any Suggestions
Thanks in Advance
You can use preg_replace to remove all special characters from your string.
Example
echo $text= str_replace(' ','|', preg_replace('/[^A-Za-z0-9 ]/', '',"This is some $123 Money"));
Output
This|is|some|123|Money
Here is the working demo for you: https://3v4l.org/M456J
<?php
$s = "This is some $123 Money";
$result = preg_replace("/[^a-zA-Z0-9]+/", "|", $s);
echo $result;
Output:
This|is|some|123|Money
This question already has an answer here:
PHP preg_replace pattern only seems to work if its wrong?
(1 answer)
Closed 8 years ago.
I already asked the question how to get the string fields from a pipe-separated string with pipe-escaping, and got an answer that worked well on http://www.regex101.com: a regex to get the single words of a pipe-separated string, supporting pipe-escaping.
Unfortunately this does not seem to work in PHP's preg_match_all() function:
$input = 'word1| word 2 |word\|3';
$pattern = '/(?P<w>(?:[^\\|]+|\\\|?)+)/';
$matches = array();
preg_match_all($pattern,$input,$matches);
// Expected $matches: $matches['w'] => array('word1', ' word 2 ', 'word\|3')
What am I missing? The example is working fine here:
https://regex101.com/r/zM7yV5/4
$re = "/(?P<w>(?:[^\\\\|]+|\\\\\\|?)+)/";
$str = "word1| word 2 |word\|3";
preg_match_all($re, $str, $matches);
This question already has answers here:
Replace Comma(,) with Dot(.) RegEx php
(5 answers)
Closed 9 years ago.
I have string
10.2, 200.3, 33.00
and I want it to be replaced as
10,2, 200,3, 33,00
I tried
preg_replace("/[.]$/","/\d[,]$/",$input);
but it is not replacing!
I can't use str_replace because it's task in university
Do not use regular expresion when dumb str_replace() suffices:
$str = str_replace('.', ',', $str)
See docs: http://php.net/str_replace
preg_replace('/\./', ',', $input);
This would replace all . dots with ,.
preg_replace('/(\d+).(\d+)/', '$1,$2', $input);
This is more specific to your need. $1 replaces first digit as in parenthesis; $2 replaces second.
You can try this
preg_replace('/[^0-9\s]/', ',', $input)
but it is better if you use
str_replace('.', ',', $input)
as Marcin answered.
This question already has answers here:
Remove Text Between Parentheses PHP
(7 answers)
Closed 9 years ago.
Say you have a string like this:
This is a string (with parenthesis stuff)
How would you change that to
This is a string
?
Replace it:
preg_replace('/\(.*?\)/', '', $str);
Use a regex.
Replace \([^)]*\) with the empty string.
Note: If there's more than one pair of parenthesis this will replace the innermost one.
try this
$string = "This is a string (with parenthesis stuff)";
echo preg_replace("/\([^)]+\)/","",$string); // 'ABC '
or you can do this also
$str = "This is a string (with parenthesis stuff)";
$str = trim(preg_replace('/\s*\([^)]*\)/', '', $str));