Strip php variable, replace white spaces with dashes
This function will create an SEO friendly string
function seoUrl($string) {
//Lower case everything
$string = strtolower($string);
//Make alphanumeric (removes all other characters)
$string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
//Clean up multiple dashes or whitespaces
$string = preg_replace("/[\s-]+/", " ", $string);
//Convert whitespaces and underscore to dash
$string = preg_replace("/[\s_]/", "-", $string);
return $string;
}
should be fine :)
Replace Dash with Space in PHP
<?php
echo '<div><a href="http://www.envisionforce.com/local/'.$row[website].'-seo-services">'.ucwords(str_replace("-"," ",$row[website])).'</a></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
Php make spaces in a word with a dash
Oldschool without regex
$test = "jjfnj 948";
$test = str_replace(" ", "", $test); // strip all spaces from string
echo substr($test, 0, 3)."-".substr($test, 3); // isolate first three chars, add hyphen, and concat all characters after the first three
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);
Replace all spaces with dashes, but not accents characters - PHP
If you are only changing spaces, in your RegEx, try matching the spaces.
$username = preg_replace("![ ]+!i", "-", $username);
I would also recommend against unescaped \
characters - they have a habit of meaning something in both PHP strings (more so with double quotes) and in RegEx.
Alternatively, \w
will match any word character, and \d
will match any number, so ![^\w\d]+!iu
will match anything that isn't alphanumeric.
$username = preg_replace('![^\\w\\d]+!iu', '-', $username);
Note, the i
makes it case-insensitive, the u
makes it unicode safe.
Example:
echo preg_replace('![^\\w\\d\\\\]+!iu', '-', 'this is a testé');
outputs this-is-a-testé
Replace comma or whitespace with hyphen in same string
$result = preg_replace('/[ ,]+/', '-', trim($value));
Test:
$value = ' home ,garden , gardener ';
$result = preg_replace('/[ ,]+/', '-', trim($value));
echo $result;
//home-garden-gardener
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>
Related Topics
Check If Current User Is Administrator in Wordpress
Replace &Nbsp; Characters That Are Hidden in Text
Importing Multiple CSV Files to MySQL Tables
Problems with Lib-Icu Dependency When Installing Symfony 2.3.X via Composer
Concatenate PHP Function Output to a String Like Variables
PHP JSON Encode Output Number as String
How Add Custom Validation Rules When Using Form Request Validation in Laravel 5
Remove Duplicated Elements of Associative Array in PHP
Google-Like Search Engine in PHP/Mysql
PHP Method="Post" Stopped Working After I Added This .Htaccess... Why
Tracking Email with PHP and Image
How to Set an Absolute Include Path in PHP
Jquery Tablesorter Index Column Insert
Combine 2-3 Transparent Png Images on Top of Each Other with PHP