Remove backslash \ from string using preg replace of php - 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);

Related

How to add single quote with Regex php arrays

For From
$data[ContactInfos][ContactInfo][Addresses]
to
$data['ContactInfos']['ContactInfo']['Addresses']
Try the following regex(Demo):
(?<=\[)|(?=\])
PHP(Demo):
preg_replace('/(?<=\[)|(?=\])/', "'", $str);
With this
preg_replace('/\[([^\]]+)\]/', "['\1']", $input);
Try it here
https://regex101.com/r/YTIOWY/1
If you have mixed string - with and without quote, regex must be a little sophysticated
$str = '$data[\'ContactInfos\'][ContactInfo]["Addresses"]';
$str = preg_replace('/(?<=\[)(?!(\'|\"))|(?<!(\'|\"))(?=\])/', "'", $str);
// result = $data['ContactInfos']['ContactInfo']["Addresses"]
demo
The first rule of regex: "Don't use regex unless you have to."
This question doesn't require regex and the function call is not prohibitively convoluted. Search for square brackets, and write a single quote on their "inner" side.
Code (Demo)
$string='$data[ContactInfos][ContactInfo][Addresses]';
echo str_replace(['[',']'],["['","']"],$string);
Output:
$data['ContactInfos']['ContactInfo']['Addresses']

Why doesn't PHP's preg_replace function like escaping "\"?

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 _+-.,!##$%^&*();/|<>

PHP Remove spaces and %20 within single function

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'=>'', ' '=>''));

PHP Regex replace tags

I have a piece of text that contains:
[shipping_address]
<p><b>#shipping_title#</b></p>
<p>#shipping_name#<br>
#shipping_streetNrBox#<br>
#shipping_zipcode# #shipping_city#<br>
#shipping_country#<br>
</p>
[/shipping_address]
In php if a certain if statements return true, I want to remove the entire block (including [shipping_address][/shipping_address]). I am using a preg_replace but I need some help with the code.
$content = preg_replace("\[shipping_address\](.*?)\[/shipping_address\]", "" , $content);
does not do the trick, can someone help me out please.
This will do the stuff:
$sData = preg_replace('/\[shipping_address\](.*?)\[\/shipping_address\]/si', '', $sData);
-be aware about using pattern delimiters and multiline replacement (s modifier - in this case, it refers to . (dot) symbol). I've also added i modifier to make replacement case-insensitive.
You should use Pattern Modifiers.
s (PCRE_DOTALL):
If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. Without it, newlines are excluded. This modifier is equivalent to Perl's /s modifier. A negative class such as [^a] always matches a newline character, independent of the setting of this modifier.
<?php
$string = '123 [shipping_address]
<p><b>#shipping_title#</b></p>
<p>#shipping_name#<br>
#shipping_streetNrBox#<br>
#shipping_zipcode# #shipping_city#<br>
#shipping_country#<br>
</p>
[/shipping_address] test';
var_dump( preg_replace('/\[shipping_address\].*\[\/shipping_address\]/s', '', $string ));
You can try this
preg_replace('/([shipping_address[^>]*])(.*?)([\/shipping_address])/i', '', $string);
If you want to remove the shippingaddress too: then try this
preg_replace('/[shipping_address[^>]*].*?[\/shipping_address]/i', '', $string);
It should work for you:
$content = preg_replace("/\[shipping_address\](.*[\n\r].*)*\[/shipping_address\]/", "" , $content);
You can try this:
$search = "/\[shipping_address\](.*?)\[\/shipping_address]/s";
$replace = " ";
$string = "[shipping_address]
<p><b>#shipping_title#</b></p>
<p>#shipping_name#<br>
#shipping_streetNrBox#<br>
#shipping_zipcode# #shipping_city#<br>
#shipping_country#<br>
</p>
[/shipping_address]";
echo preg_replace($search,$replace,$string);

How to remove the probable dot at the beginning/end of string in PHP?

I tried this:
echo preg_replace('/[^,,$]/', '', ',test,hi,');
But gets:
,,,
Do you mean
preg_replace('/^,|,$/', '', ',test,hi,');
? Inside a character class […], a leading ^ means negation, and $ doesn't have any special meanings.
You could use the trim function instead.
trim(',test,hi,', ',');
preg_replace is a bit overkill
$string = ',,ABCD,EFG,,,,';
$newString trim($string,',');
trim(',test,hi,',','); // echoes test,hi

Categories