I have something like this :
$x = "#frac {2} {3}";
I try this code but is doesnt work:
$x = str_replace("#","\"",$x);
I want to replace # with with \ in this string. But I cant use str_replace.
any help?
You are replacing the # with a "
To use the backslash in a string you need to escape it.
How do you make a string in PHP with a backslash in it?
$x = "#frac {2} {3}";
$x = str_replace("#", "\\", $x);
Related
I have a string with the format:
$string = 'First\Second\Third';
'First' and 'Second' are always the same but Third' is just and an example (can be anything but in with special character '\' between them).
I want to create a string like that:
$string = 'Second_Third';
I tried to use function preg_replace and this is my code:
preg_replace(Array('/^First\\\/', '/Second\\\[^\s]+/'), Array('', '/Second\_[^\s]+/'), $string);
I have no idea how to do this.
10x
You can use:
$str = preg_replace('/.*?\\\\(Second)\\\\(.+)$/', '$1_$2');
RegEx Demo
If you're just looking for \ then you don't need a regular expression. You can do a regular character match. Using list and explode you can do it like so:
list($first, $second) = explode("\\", "First\\Second\\Third");
echo $first . "_" . $second;
Note that you need to escape \ because it's the escape character :>
I'm trying to get the parent "elements" name as string from a PHP namespace in a string, the ideia is to do:
Input: \Base\Util\Review; Desired Output: \Base\Util;
My main problem here is how can I deal with the backslash escaping in the regex expression, so far I can make it work with the normal slash:
$ns = "/Base/Util/Review";
print preg_replace("#\/[^/]*$#", '', $ns);
// Outputs => /Base/Util
Thank you.
This should work:
$s = '\\Base\\Util\\Review';
$r = preg_replace('~\\\\[^\\\\]*$~', '', $s);
//=> \Base\Util
You can do it without preg_replace
$ns = "\Base\Util\Review";
print implode('\\', array_slice(explode('\\',$ns),0,-1));
Another option:
$ns = "\Base\Util\Review";
print substr($ns,0,strrpos($ns,'\\'));
Try this
print preg_replace("#\\\[^\\\]*$#", '', $ns)
You will have to write four backslashes for each literal backslash:
$regex = '#\\\\#'; // regex matching one backslash
You need to escape each \ once to escape its special meaning in the regex, and again escape each \ once to escape its meaning in the PHP string literal.
I have to match php variable in the php file using preg_match()
$GLOBALS['app_list_strings']['enjay_host_list'] = array (
How can I do this.
I am doing,
<?php
$filename='/var/www/su/custom/include/language/en_us.lang.php';
$fileopen=file($filename);
//echo $fileopen[2];
$NoOflines = count($fileopen);
echo $NoOflines ."<br>";
$Changed=0;
$Foundon=0;
$FoundFirstClose=0;
for($i=0;$i<$NoOflines;$i++)
{
echo $fileopen[$i]."<br>";
if(preg_match("/\$GLOBALS['app_list_strings']['enjay_host_list']=array ( /i", $fileopen[$i]))
{
$Foundon=$i;
echo $fileopen[$i]."<br>";
}
}
?>
You need to escape every character that has a special meaning in a regexp, which means not only the $ but also [, ] and (. See the PCRE documentation for the list of special characters in pcre regexp.
Another issue is that because you use double quotes, php tries to replace $GLOBALS.. with a variable content unless you double backslash it on top of it, so it's better to just use the Nowdoc syntax (if you use php >= 5.3, which you really should).
$pattern = <<<'EOS'
/\$GLOBALS\['app_list_strings'\]\['enjay_host_list'\]=array \( /i
EOS;
for($i=0;$i<$NoOflines;$i++)
{
echo $fileopen[$i]."<br>";
if(preg_match($pattern, $fileopen[$i]))
{
$Foundon=$i;
echo $fileopen[$i]."<br>";
}
}
your missing some escape characters in your regular expression.
"/\$GLOBALS\['app_list_strings'\]\['enjay_host_list'\]=array\s\(/i"
$string = "\$GLOBALS['app_list_strings']['enjay_host_list']=array (" ;
$match = preg_match("/\\\$GLOBALS\['app\_list\_strings'\]\['enjay\_host\_list'\]\s*=\s*array\s*\(/i", $string);
var_dump($match) ;
Possible issues with your regexp:
You have to escape special chars as ([]_$)
You have to double escape $ sign when using double quotes in PHP
Whitespaces are also important. I used \s* which correspons to 0 or more whitespaces.
I am after (if possible), a reg expression which will only replace questions marks "?" with "'" where it is not followed by the "=" symbol in my string?
e.g. this is something?, but this will remain?=forever
should end up as:
this is something', but this will remain?=forever
Thanks
Mu
This is simple using a negative lookahead: Use
\?(?!=)
Just check this.. Simple one without regex ( I don't know regex :( )...:p
$string = "this is something?, but this will remain?=forever";
$my_secret_replace = "THISISANYTHINGWHICHWILLNOTINSTRING_OR_ANYRANDOMNUMBER";
$temp_string = str_replace("?=",$my_secret_replace,$string);
$temp_string = str_replace("?","'",$temp_string);
$final_string = str_replace($my_secret_replace,"?=",$temp_string);
$string = "String contains ? and ?= contains too?=?";
echo preg_replace("/\?([^=]|$)/", "'\\1", $string);
I have a string like this: foo($bar1, $bar2)
How to I replace each variable with <span>$variable</span> with regexp?
This is my try (not working):
$row['name'] = preg_replace("/\$\w+/S", "<span>$1</span>", $row['name']);
I only want the variables to be replaced and have a span around them, I don't want commas or spaces to be replaced.
What I want is to have my string foo($bar1, $bar2) to be replaced with foo(<span>$bar1</span>, <span>$bar2</span>) ($bar1 and $bar2 are not variables, it's plain text).
Here are some problems I can see:
Since you are using double quotes for the regex,
you need to escape the $ using two \ as \\$.
Alternatively you can just use single
quote and use \$.
You are using $1 in the replacement
but you are not having any group in
the regex. So have ( ) around
\$\w+
So try:
$str = preg_replace('/(\$\w+)/', "<span>$1</span>", $str);
or
$str = preg_replace("/(\\$\w+)/", "<span>$1</span>", $str);
See it.