find \ back slash and remove it from sting php - php

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!

Related

How to add slash to a special character in PHP?

I am trying to add a back slash to a string having special character.
My input is:
db:xz~sf!fkd#djf#dfs$dlf%dks^kd&fkf*kdf(dls)kls-fls+fsd=slf_fls[fdf]fdf{ffl}sl|dkf\fsl'skfj
And my output should be:
db\:xz\~sf\!fkd\#djf\#dfs\$dlf\%dks\^kd\&fkf\*kdf\(dls\)kls\-fls\+fsd\=slf\_fls\[fdf\]fdf\{ffl\}sl\|dkf\\fsl\'skfj
And I have the following piece of code which is only replacing the special character with the back slash character:
<?php
echo $string = "db:xz~sf!fkd#djf#dfs$dlf%dks^kd&fkf*kdf(dls)kls-fls+fsd=slf_fls[fdf]fdf{ffl}sl|dkf\fsl'skfj";
echo preg_replace('/[^A-Za-z0-9\-]/', '\\', $string);
So could someone help me regarding this?
You can use:
$s = 'db:xz~sf!fkd#djf#dfs$dlf%dks^kd&fkf*kdf(dls)kls-fls+fsd=slf_fls[fdf]fdf{ffl}sl|dkf\fsl\'skfj';
echo preg_replace('/\W/', '\\\\$0', $s)
//=> db\:xz\~sf\!fkd\#djf\#dfs\$dlf\%dks\^kd\&fkf\*kdf\(dls\)kls\-fls\+fsd\=slf_fls\[fdf\]fdf\{ffl\}sl\|dkf\\fsl\'skfj

How to replace backslashes with forward slashes without changing the content of the link

There is an old question from 2011 but with a wrong answer.
Maybe now someone can give a better answer. The question was: How can we replace the backslashes with forward slashes from this variable-link:
$str = "http://www.domain.com/data/images\flags/en.gif";
The wrong answer was that:
echo $str = str_replace('\\', '/', $str);
And it was wrong because the result of that code changes the content of the link. (http://www.domain.com/data/imageslags/en.gif)
It finds not only the backslash but the letter f after that and it deletes them because \f means "formfeed (hex 0C)". So how can we avoid this wrong replacement?
The Code is here
i hope this helps you
$str = "http://www.domain.com/data/images\flags/en.gif";
echo $str = str_replace('lags', "/flags", $str);
You can use addcslashes but you'll have to specify each possible escaped character that could occur in $str
$str = "http://www.domain.com/data/images\flags/en.gif";
$escaped = str_replace("\\","/",addcslashes($str,"\f\r\n\t"));
echo $escaped; // result is 'http://www.domain.com/data/images/flags/en.gif'

PHP: preg_replace() on NameSpace

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.

Remove backslash \ from string using preg replace of php

I want to remove the backslash alone using php preg replace.
For example: I have a string looks like
$var = "This is the for \testing and i want to add the # and remove \slash alone from the given string";
How to remove the \ alone from the corresponding string using php preg_replace
why would you use preg_replace when str_replace is much easier.
$str = str_replace('\\', '', $str);
To use backslash in replacement, it must be doubled (\\\\ PHP string) in preg_replace
echo preg_replace('/\\\\/', '', $var);
You can also use stripslashes() like,
<?php echo stripslashes($var); ?>
$str = preg_replace('/\\\\(.?)/', '$1', $str);
This worked for me!!
preg_replace("/\//", "", $input_lines);

PHP preg_replace

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;

Categories