Using PHP Replace Spaces in Urls with %20

Using PHP Replace SPACES in URLS with %20

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.

Replace spaces with a Underscore in a URL

Try using str_replace for replace space with underscore

<div class="groupinfo">
<a href="<?= base_url() ?>group/group_id/<?= $gr->grp_id ?>/<?= str_replace(" ","_",$gr->grp_name) ?>">
</div>

PHP str_replace replace spaces with underscores

I'll suggest that you use this as it will check for both single and multiple occurrence of white space (as suggested by Lucas Green).

$journalName = preg_replace('/\s+/', '_', $journalName);

instead of:

$journalName = str_replace(' ', '_', $journalName);

Need to remove space to create URL?

The str_replace function allows you to replace characters in a string and strtolower will make the whole thing lowercase:

<?php foreach ($cities as $city):?>
<li>
<a href="city-<?php echo strtolower( str_replace(' ', '-', $city['name']) ); ?>">
<?php echo $city['name']; ?>
</li>
<?php endforeach;?>

http://php.net/manual/en/function.str-replace.php

http://php.net/manual/en/function.strtolower.php



Related Topics



Leave a reply



Submit