Viewed   358 times

I'm looking to replace all instances of spaces in urls with %20. How would I do that with regex?

Thank you!



No need for a regex here, if you just want to replace a piece of string by another: using str_replace() should be more than enough :

$new = str_replace(' ', '%20', $your_string);

But, if you want a bit more than that, and you probably do, if you are working with URLs, you should take a look at the urlencode() function.

Thursday, October 13, 2022

There's no need to use a regex for this. PHP has an inbuilt function to do just this. Use parse_url():

$domain = parse_url($url, PHP_URL_HOST);
Tuesday, December 27, 2022

Here is yet another way to do this and preserve your spaces:

$my_string = " 4444/8888/7777";
$string_array = explode(' ', $my_string);
print_r($string_array); // for testing

$new_array = '';
foreach($string_array AS $original) {
    $pos = strpos($original, 'http');
    if(0 === $pos){
        $new = preg_replace('/(?<=d)/(?=d)/', '', $original);
        $new_array[] = $new;
    } else {
        $new_array[] = $original;
$new_string = implode(' ', $new_array);
echo $new_string;

Returns (note the preserved spaces): 4444/8888/7777

EDIT - Pure regex method:

$new_string = preg_replace('/(?<=/d)(/)/', '', $my_string);
echo $new_string;

Returns: 4444/8888/7777

CAVEATS: a. ) works even if there are no spaces in the string 2. ) does not work if any number between / is more than one digit in length. iii. ) if the second group of digits is like 4444/5/8888 the second slash would get removed here too.

Here is how the regex breaks down:

Using a positive lookbehind to match a / followed by a digit (?<=/d) I can assert what I am looking for - I only want to remove the forward slashes after a forward slash followed by a digit. Therefore I can capture the other forward slashes with (/) immediately after the lookbehind. There is no need to include http:// to start or .jpg to close out.

Sunday, October 30, 2022

You can use this regex:



$arr = array(
'This is really cool site: isn't it?',
'Hello! You can visit for some awesome pictures, or just go to by itself',
'Hello! Check out my site at!',
'Hello.Come visit!',

foreach($arr as $url) {   
   $link = preg_replace_callback('#(s|^)((?:https?://)?w+(?:.w+)+(?<=.(net|org|edu|com))(?:/[^s]*|))(?=s|b)#is',
           create_function('$m', 'if (!preg_match("#^(https?://)#", $m[2]))
               return $m[1]."<a href="http://".$m[2]."">".$m[2]."</a>"; else return $m[1]."<a href="".$m[2]."">".$m[2]."</a>";'),
   echo $link . "n";


<a href=""></a>
This is really cool site: <a href=""></a>/ isn't it?
<a href=""></a>
<a href=""></a>
Hello! You can visit <a href=""></a> for some awesome pictures, or just go to <a href=""></a> by itself
<a href=""></a>
<a href=""></a>
Hello! Check out my site at <a href=""></a>!
Hello.Come visit <a href=""></a>!
<a href=""></a>

PS: This regex only supports http and https scheme in URL. So eg: if you want to support ftp also then you need to modify the regex a little.

Sunday, August 7, 2022

The newline could still be registering if you've got the "rn" version of a newline - you're removing "n" and leaving "r" behind.

$str = preg_replace('/s+/',',',str_replace(array("rn","r","n"),' ',trim($str)));

Cleaner (more legible) version:

$str = preg_replace('#s+#',',',trim($str));
Friday, October 21, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :