PHP Remove spaces and %20 within single function - php

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

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']

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);

Replace Dash with Space in PHP

I currently have this line in my code:
<div>'.ucwords($row[website]).'</div>
And it will display a city name such as this:
Puiol-del-piu
But what I need is for it to display without the dashes and so that ucwords will capitalize the first letter of each word such as this:
Puiol Del Piu
It would be great if the code could be confined to this one line because I have a lot more going on with others stuff in the page as well.
This str_replace does the job:
$string = str_replace("-", " ", $string);
Also, you can make it as a function.
function replace_dashes($string) {
$string = str_replace("-", " ", $string);
return $string;
}
Then you call it:
$varcity = replace_dashes($row[website]);
<div>'.ucwords($varcity).'</div>
<?php
echo '<div>'.ucwords(str_replace("-"," ",$row[website])).'</div>';
In the above example you can use str_replace() to replace hyphens with spaces to create separate words. Then use ucwords() to capitalize the newly created words.
http://php.net/manual/en/function.str-replace.php
http://php.net/manual/en/function.ucwords.php
replace dash with space
str_replace("-"," ",$row[text])
replace space with dash
str_replace(" ","-",$row[text])
str_replace ('Find what you want to replace', 'Replace with ', 'Your array or string variable');
If you want to replace dash with space, you can use this:
str_replace("-"," ",$row[text])
If you want to replace space with dash, use this:
str_replace(" ","-",$row[text])
Use ucwords() to capitalize words.

How to define pattern in preg_replace to remove spaces?

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);

str_replace spaces with hyphens in A tag's name attribute

$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.

Categories