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.
Related
i have full string like this:
Route::post('asdasdasdad/{param1}/{param2}', 'Admin\RouteController#a212e12e');.
and want to delete that route so in preg_replace i focus on
Route::post('asdasdasdad as start text and
Admin\RouteController#a212e12e'); as last text.
here what i try
preg_replace("/Route::post('asdasdasdad\(.*Admin\RouteController#a212e12e');\s*/s", "", $string);
but its not working.
you have some errors in your regex, some un-escaped regex characters. try this
preg_replace("/Route::post\('asdasdasdad.*Admin\\\\RouteController#a212e12e'\);\s*/s", "", $string);
if you want to replace multiple lines in one go
preg_replace_all("/Route::post\('asdasdasdad.*Admin\\\\RouteController#a212e12e'\);\s*/s", "", $string);
witch works as if you add the multi line modifier to your regex
$string = file_get_contents('route.php');
$string = preg_replace("/Route::post\('asdasdasdad.*Admin\\\\RouteController#a212e12e'\);\s*/s", "", $string);
echo $string;
you get the line with EOL removed
I have a load of labels which are camel case. Some examples are
whatData
whoData
deliveryDate
importantQuestions
What I am trying to do is this. Any label which has the word Data needs to have this word removed. At the point of the capital letter, I need to provide a space. Finally, everything should be uppercase. I have done the removal of Data and the uppercase by doing this ($data->key is the label)
strtoupper(str_replace('Data', '', $data->key))
The part I am struggling with is adding the spaces between words. So basically the above words should end up like this
WHAT
WHO
DELIVERY DATE
IMPORTANT QUESTIONS
How can I factor in the last part of this?
Thanks
It will add spaces before every capital letters. Try this:
$String = 'whatData';
$Words = preg_replace('/(?<!\ )[A-Z]/', ' $0', $String);
Problem
Your regex '~^[A-Z]~' will match only the first capital letter. Check out Meta Characters in the Pattern Syntax for more information.
Your replacement is a newline character '\n' and not a space.
Solution
Use preg_replace(). Try below code.
$string = "whatData";
echo preg_replace('/(?<!\ )[A-Z]/', ' $0', $string);
Output
what Data
Try following:
$string = 'importantQuestions';
$string = strtoupper(ltrim(preg_replace('/[A-Z]/', ' $0', $string)));
echo $string;
This will give you output as:
IMPORTANT QUESTIONS
Try this:
preg_split: split on camel case
array_map: UPPER case all the element
implode: Implode the array
str_replace: Replace the `DATE` with empty
trim: trim the white spaces.
Do this simple things:
echo trim(str_replace("DATE", "", implode(" ", array_map("strtoupper", preg_split('/(?=[A-Z])/', 'deliveryDate', -1, PREG_SPLIT_NO_EMPTY))))); // DELIVERY
This is result exactly what you want.
I wish to remove white space from a string. The string would have ben urlencoded() prior, so I also wish to remove %20 too. I can do this using two separate functions, but how do i do this with one function?
$string = str_replace("%20","",$string);
$string = str_replace(" ","",$string);
You could use preg_replace function.
preg_replace('~%20| ~', "", $string)
Don't use a regex for that but strtr:
$result = strtr($str, array('%20'=>'', ' '=>''));
I'm trying to remove all words of less than 3 characters from a string, specifically with RegEx.
The following doesn't work because it is looking for double spaces. I suppose I could convert all spaces to double spaces beforehand and then convert them back after, but that doesn't seem very efficient. Any ideas?
$text='an of and then some an ee halved or or whenever';
$text=preg_replace('# [a-z]{1,2} #',' ',' '.$text.' ');
echo trim($text);
Removing the Short Words
You can use this:
$replaced = preg_replace('~\b[a-z]{1,2}\b\~', '', $yourstring);
In the demo, see the substitutions at the bottom.
Explanation
\b is a word boundary that matches a position where one side is a letter, and the other side is not a letter (for instance a space character, or the beginning of the string)
[a-z]{1,2} matches one or two letters
\b another word boundary
Replace with the empty string.
Option 2: Also Remove Trailing Spaces
If you also want to remove the spaces after the words, we can add \s* at the end of the regex:
$replaced = preg_replace('~\b[a-z]{1,2}\b\s*~', '', $yourstring);
Reference
Word Boundaries
You can use the word boundary tag: \b:
Replace: \b[a-z]{1,2}\b with ''
Use this
preg_replace('/(\b.{1,2}\s)/','',$your_string);
As some solutions worked here, they had a problem with my language's "multichar characters", such as "ch". A simple explode and implode worked for me.
$maxWordLength = 3;
$string = "my super string";
$exploded = explode(" ", $string);
foreach($exploded as $key => $word) {
if(mb_strlen($word) < $maxWordLength) unset($exploded[$key]);
}
$string = implode(" ", $exploded);
echo $string;
// outputs "super string"
To me, it seems that this hack works fine with most PHP versions:
$string2 = preg_replace("/~\b[a-zA-Z0-9]{1,2}\b\~/i", "", trim($string1));
Where [a-zA-Z0-9] are the accepted Char/Number range.
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);