I use netbeans, I try to replace \ with \\ but it fails , it can't escape the \\ character.
This is not a Netbeans issue , it's a PHP issue .
preg_replace('\','\\','text to \ be parsed');
Any sollutions?
Use 4 backslashes and please don't forget the delimiters:
echo echo preg_replace('~\\\\~','\\\\\\\\','text to \\ be parsed');
Online demo
Explanation: When PHP parse \\\\ it will escape \\ two times, which means it becomes \\, now when PHP passes it to the regex engine, it will receive \\ which means \.
Try the php chr() function and tell the preg_replace the char ascii code for \ and \\.
chr function
ascii code table
<?php
echo chr(52) . "<br>"; // Decimal value
echo chr(052) . "<br>"; // Octal value
echo chr(0x52) . "<br>"; // Hex value
preg_replace(chr(1),chr(2),'text'),
?>
This works: (using str_replace() rather than preg_replace())
$str = "text to \ be parsed";
$str = str_replace('\\', '\\\\', $str);
echo $str;
Related
I have:
$input = str_replace('/all these symbols/', "", $input);
Can't really understand the pattern syntax for preg_match and preg_replace. Sometimes people use '+ - * ^ $ \s' and different kind of brackets there. Tried to read the manual, but don't really get it. Can i find somewhere more clear information about all the possibilities of the preg syntax? Thanks in advance.
Just put those characters into character class as shown in below example:
$replaced = preg_replace("/[“”!?;\",.\/”“']/u", " ", "hello!?; “how are you”");
print_r($replaced);
The output:
hello how are you
You can use squared brackets, just have to escape the right chars:
echo preg_replace('/[“”!?;",.\/”“\']/', '', 'a!b?c');
I am trying to find and replace binary values in strings:
$str = '('.chr(0x00).chr(0x91).')' ;
$str = preg_replace("/\x00\x09/",'-',$str) ;
But I am getting "Warning: preg_replace(): Null byte in regex" error message.
How to work on binary values in Regex/PHP?
It is because you are using double quotes " around your regex pattern, which make the php engine parse the chars \x00 and \x09.
If you use single quotes instead, it will work:
$str = '(' . chr(0x00) . chr(0x91) . ')' ;
$str = preg_replace('/\x00\x09/', '-', $str) ;
But your regex seems also not to be correct, if I understood your question correctly. If you want to replace the chars \x00 and \x91 with a dash -, you must put them into brackets []:
$str = preg_replace('/[\x00\x91]/', '-', $str) ;
I have a problem with replace \ from string
<?php $postlink = str_replace( "\" , '', $postlink); ?>
what's wrong with that line dreamweaver tell me that wrong line
\ is a special character you need to use \\
<?php
$postlink = str_replace( "\\" , '', $postlink);
?>
You can also use stripslashes() for removing slash, why are you str_replace()? if you have only backslashes than use stripslashes().
Example:
echo stripcslashes("it\'s working day!"); //it's working day!
I use the RegExr website to test my regular expressions. On that website it can easily find the character "\" with the RegEx string /\\/g. But when I use it in php it throws back:
Warning: preg_replace(): No ending delimiter '/'
My Code
$str = "0123456789 _+-.,!##$%^&*();\/|<>";
echo preg_replace('/\\/', '', $str);
Why doesn't PHP like to escape "\"?
When using it in the regexp use \\ to use it in the replacement, use \\\\ will turn into \\ that will be interpreted as a single backslash.
Use it like this:
<?php
$str = "0123456789 _+-.,!##$%^&*();\/|<>";
echo preg_replace('/\\\\/', '', $str);
Output:
0123456789 _+-.,!##$%^&*();/|<>
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.