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
Can't Pass MySQLi Connection in Session in PHP
Fatal Error: Using $This When Not in Object Context
Looping a Multidimensional Array in PHP
Array_Multisort and Dynamic Variable Options
How to Get System Environment Variables into PHP While Running Cli & Apache2Handler
How to Read a List of Files from a Folder Using PHP
Traits in PHP - Any Real World Examples/Best Practices
PHP Regex to Check Date Is in Yyyy-Mm-Dd Format
PHP Case-Insensitive In_Array Function
Regular Expression to Collect Everything After the Last /
Understanding Nested PHP Ternary Operator