I have a string like this.
$string="howto old_iccid = 01920930123 new_iccid =102930123"
How to remove space character in order to get string like this:
$string="howto old_iccid=01920930123 new_iccid=102930123"
I have used preg_replace, but still not understand to define the pattern which is handling those things..
How to define pattern in preg_replace to remove spaces?
$string = "howto old_iccid = 01920930123 new_iccid =102930123";
$string = preg_replace('/\s*=\s*/', '=', $string);
var_dump($string);
Related
I'm trying to use preg_match just for string inside two certain symbols for example
My Name is %^i%Ibrahem ^i.
I want to use preg_match just for ^i which is inside this two symbols %% to be font-style:italic; I've tried:
$find=array('`\^i`si');
$replace=array('font-style:italic;');
$replaced = preg_replace($find,$replace,$string);
but it replaces the last ^i too also keep in mind that the string between %% can be also %^b ^i% so I can't condition that the string have to be %^i% Help!
You could use this pattern instead:
$find = array('/%(.*?)(\^i)(.*?)%/', '/%(.*?)(\^b)(.*?)%/');
$replace = array('%$1font-style:italic;$3%', '%$1font-weight:bold;$3%');
$replaced = preg_replace('/%(.*)%/', '<span style="$1">', preg_replace($find, $replace, $string));
i've scraped a html string from a website. In this string it contains multiple strings like color:#0269D2. How can i make str_replace code which replace this string with another color ?
For instance something like this just looping through all color:#0269D in the fulltext string variable?
str_replace("color:#0269D","color:#000000",$fulltext);
you pass array to str_replace function , no need to use loop
$a= array("color:#0269D","color:#000000");
$str= str_replace($a,"", $string);
You have the right syntax. I would add a check:
$newText = str_replace("color:#0269D", "color:#000000", $fulltext, $count);
if($count){
echo "Replaced $count occurrences of 'color'.";
}
This code might be too greedy for what you're looking to do. Careful. Also if the string differs at all, for example color: #0269D, this replacement will not happen.
’str_replace’ already replaces all occurrences of the search string with the replacement string.
If you want to replace all colors but aren't sure which hexcodes you'll find you could use preg_replace to match multiple occurrences of a pattern with a regular expression and replace it.
In your case:
$str = "String with loads of color:#000000";
$pattern = '/color ?: ?#[0-9a-f]{3,6}/i';
$replacement = "color:#FFFFFF";
$result = preg_replace($pattern, $replacement, $str);
I wish to remove white space from a string. The string would have ben urlencoded() prior, so I also wish to remove %20 too. I can do this using two separate functions, but how do i do this with one function?
$string = str_replace("%20","",$string);
$string = str_replace(" ","",$string);
You could use preg_replace function.
preg_replace('~%20| ~', "", $string)
Don't use a regex for that but strtr:
$result = strtr($str, array('%20'=>'', ' '=>''));
I have a string that contains many underscores followed by words ex: "Field_4_txtbox" I need to find the last underscore in the string and remove everything following it(including the "_"), so it would return to me "Field_4" but I need this to work for different length ending strings. So I can't just trim a fixed length.
I know I can do an If statement that checks for certain endings like
if(strstr($key,'chkbox')) {
$string= rtrim($key, '_chkbox');
}
but I would like to do this in one go with a regex pattern, how can I accomplish this?
The matching regex would be:
/_[^_]*$/
Just replace that with '':
preg_replace( '/_[^_]*$/', '', your_string );
There is no need to use an extremly costly regex, a simple strrpos() would do the job:
$string=substr($key,0,strrpos($key,"_"));
strrpos — Find the position of the last occurrence of a substring in a string
You can also just use explode():
$string = 'Field_4_txtbox';
$temp = explode('_', strrev($string), 2);
$string = strrev($temp[1]);
echo $string;
As of PHP 5.4+
$string = 'Field_4_txtbox';
$string = strrev(explode('_', strrev($string), 2)[1]);
echo $string;
$string = preg_replace("#[name=([a-zA-Z0-9 .-]+)*]#",''."$1",$string);
This part of script doesn't work:
str_replace(' ', '-', "$1")
I need to replace " " with "-",
i also try preg_replace inside main preg_replace, str_ireplace also
But this is still don't working
The replacement is evaluated upfront and not on each replace. But you can do so by either using the e modifier in your regular expression:
$string = preg_replace("#\[name=([a-zA-Z0-9 .-]+)*]#e", '"<td>$1</td>"', $string);
Or by using preg_replace_callback:
function callbackFunction($match) {
global $front_page;
return '<td>'.$match[1].'</td>';
}
$string = preg_replace_callback("#\[name=([a-zA-Z0-9 .-]+)*]#", 'callbackFunction', $string);
I guess you will have to do it in two steps, since $1 cannot be used in str_replace(). $1 doesn’t really exist as a variable, it is only a placeholder in the replacement string.