in my functions.php if have this code:
echo ''.urldecode($search).'';
this removes the special chars..
but how can i additonally add remove space and replace it with - and remove "
so, if someone types in "yo! here" i want yo-here
Try:
<?php
$str = '"yo! here"';
$str = preg_replace( array('/[^\s\w]/','/\s/'),array('','-'),$str);
var_dump($str); // prints yo-here
?>
If you want to replace a run of unwanted characters with a single dash, then you can use something like this:
preg_replace('/\W+/', '-', $search);
To remove surrounding quotes, and then replace any other junk with dashes, try this:
$no_quotes = preg_replace('/^"|"$/', '', $search);
$no_junk = preg_replace('/\W+/', '-', $no_quotes);
This will replace multiple spaces / "special" chars with a single hyphen. If you don't want that, remove the "+".
You might want to trim off any trailing hyphens, should something end with an exclamation point / other.
<?php
preg_replace("/\W+/", "-", "yo! here check this out");
?>
You can remove any non-words:
preg_replace('/\W/', '-', $search);
Related
How can I put spaces to a long string that does not have spaces
Example : 5Bedroom.Apartment,in.NewYork>City
I want to put spaces after any dot and comma. Only if no space after dot and comma. If already have space, just ignore
you should replace the charector which u want
$str = preg_replace('/(?<!\d),|,(?!\d{3})/', ', ', $str);
Such regex ~(?<=[,.])(?=\S)~ matches position after comma or dot before not space
$str = preg_replace( ~(?<=[,.])(?=\S)~, " ", $str);
demo
I'm making a function that that detect and remove all trailing special characters from string. It can convert strings like :
"hello-world"
"hello-world/"
"hello-world--"
"hello-world/%--+..."
into "hello-world".
anyone knows the trick without writing a lot of codes?
Just for fun
[^a-z\s]+
Regex demo
Explanation:
[^x]: One character that is not x sample
\s: "whitespace character": space, tab, newline, carriage return, vertical tab sample
+: One or more sample
PHP:
$re = "/[^a-z\\s]+/i";
$str = "Hello world\nhello world/\nhello world--\nhellow world/%--+...";
$subst = "";
$result = preg_replace($re, $subst, $str);
try this
$string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
or escape apostraphe from string
preg_replace('/[^A-Za-z0-9\-\']/', '', $string); // escape apostraphe
You could use a regex like this, depending on your definition of "special characters":
function clean_string($input) {
return preg_replace('/\W+$/', '', $input);
}
It replaces any characters that are not a word character (\W) at the end of the string $ with nothing. \W will match [^a-zA-Z0-9_], so anything that is not a letter, digit, or underscore will get replaced. To specify which characters are special chars, use a regex like this, where you put all your special chars within the [] brackets:
function clean_string($input) {
return preg_replace('/[\/%.+-]+$/', '', $input);
}
This one is what you are looking for. :
([^\n\w\d \"]*)$
It removes anything that is not from the alphabet, a number, a space and a new line.
Just call it like this :
preg_replace('/([^\n\w\s]*)$/', '', $string);
I am working with a slug function and I dont fully understand some of it and was looking for some help on explaining.
My first question is about this line in my slug function $string = preg_replace('# +#', '-', $string); Now I understand that this replaces all spaces with a '-'. What I don't understand is what the + sign is in there for which comes after the white space in between the #.
Which leads to my next problem. I want a trim function that will get rid of spaces but only the spaces after they enter the value. For example someone accidentally entered "Arizona " with two spaces after the a and it destroyed the pages linked to Arizona.
So after all my rambling I basically want to figure out how I can use a trim to get rid of accidental spaces but still have the preg_replace insert '-' in between words.
ex.. "Sun City West " = "sun-city-west"
This is my full slug function-
function getSlug($string){
if(isset($string) && $string <> ""){
$string = strtolower($string);
//var_dump($string); echo "<br>";
$string = preg_replace('#[^\w ]+#', '', $string);
//var_dump($string); echo "<br>";
$string = preg_replace('# +#', '-', $string);
}
return $string;
}
You can try this:
function getSlug($string) {
return preg_replace('#\s+#', '-', trim($string));
}
It first trims extra spaces at the beginning and end of the string, and then replaces all the other with the - character.
Here your regex is:
#\s+#
which is:
# = regex delimiter
\s = any space character
+ = match the previous character or group one or more times
# = regex delimiter again
so the regex here means: "match any sequence of one or more whitespace character"
The + means at least one of the preceding character, so it matches one or more spaces. The # signs are one of the ways of marking the start and end of a regular expression's pattern block.
For a trim function, PHP handily provides trim() which removes all leading and trailing whitespace.
Ok so I am taking a string, querying a database and then must provide a URL back to the page. There are multiple special characters in the input and I am stripping all special characters and spaces out using the following code and replacing with HTML "%25" so that my legacy system correctly searches for the value needed. What I need to do however is cut down the number of "%25" that show up.
My current code would replace something like
"Hello. / there Wilbur" with "Hello%25%25%25%25there%25Wilbur"
but I would like it to return
"Hello%25there%25Wilbur"
replacing multiples of the "%25" with only one instance
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
return preg_replace('/[^A-Za-z0-9]/', '%25', $string); // Replaces special chars.
Just add a + after selecting a non-alphanumeric character.
$string = "Hello. / there Wilbur";
$string = str_replace(' ', '-', $string);
// Just add a '+'. It will remove one or more consecutive instances of illegal
// characters with '%25'
return preg_replace('/[^A-Za-z0-9]+/', '%25', $string);
Sample input: Hello. / there Wilbur
Sample output: Hello%25there%25Wilbur
This will work:
while (strpos('%25%25', $str) !== false)
$str = str_replace('%25%25', '%25', $str);
Or using a regexp:
preg_replace('#((?:\%25){2,})#', '%25', $string_to_replace_in)
No looping using a while, so the more consecutive '%25', the faster preg_replace is against a while.
Cf PHP doc:
http://fr2.php.net/manual/en/function.preg-replace.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.