I have a form where people enter their multiple codes.
Now, I would like to delete the spaces between these codes.
However, my code doesnt seem to work. Any ideas?
$codes = $_GET['codes'];
$spaces = strpos($codes, " ");
for($spaces; $spaces=0; $spaces--){
str_replace(" ", "", $codes);
echo $codes;
}
EDIT: I just have tried something else but it still doesnt work at all. I mean the echo gives me the original string every single time.
$codes = $_GET['codes'];
$cleancodes = str_replace(" ", "", $codes);
$cleancodes = trim(preg_replace('/\s\s+/', ' ', $cleancodes));
echo "<br / >" . $cleancodes;
$string = str_replace(' ', '', $string);
$text=str_replace(" ","",$text);
But doing that for code? Bound to break (if you meant program code)!
Use str_replace():-
<?php
$_GET['codes'] = "abc def ghi jkl mno pqr stu vwx yz ";
$codes = $_GET['codes'];
$codes = str_replace(" ","",$codes);
echo $codes;
Output:-https://eval.in/395364
Related
I have a string which contains n\ by mistake (as imported from csv). So,I just want to replace n\ with \n.
Possible conditions : n\,\n\n, n\\n,n\n\
$string = "hello\n how n\n\ are you?\n\nis everything\nn\ok buddy, n\ where have you been. \n";
Try this. It will remove all the possible matches as per your question:-
$str = "main_string_goes_here";
$replace = "n\,\n\n,n\n\,\nn\,n\\n";
$arr = explode(",",$replace);
foreach($arr as $value)
{
str_replace($value,"\n",$str);
}
Happy Coding :-)
Use this:
str_replace("n\","\n",$string);
Here we search for the string, find the value "n\" and then replace the value with "\n". Update this example with your conditions.
Try like this
$string = 'hello\n how n\n\ are you?\n\nis everything\nn\ok buddy, n\ where have you been. \n';
$string = str_replace('n\\n\\','&new*',$string);
$string = str_replace('\\n\\n','&old*',$string);
$string = str_replace('n\\','\\n',$string);
$string = str_replace('&new*','\\n\\n',$string);
echo $string = str_replace('&old*','\\n\\n',$string);
Live demo : https://eval.in/904353
As other says to replace "n\" to "\n" will not work. You need to escape \ backslash also
Use this:
<?php
echo $string = 'hello\n how n\n\ are you?\n\nis everything\nn\ok buddy, n\ where have you been. \n';
$new_str = preg_replace('~[\\\\/:*?"<>|]~', ' ', $string);
echo $text = str_replace('n', '', $new_str);
?>
You can try this.
$string = 'hello\n how n\n\ are you?\n\nis everything\nn\ok buddy, n\ where have you been. \n';
echo str_replace('n\\', '\n',$string );
I had an value in database like "demo text" . I want to display this content from the db in the view page as
Html code that i am using is like this <h2>Demo<span>Text</span></h2> , is there any solution for seperate each words and use one for h2 and other for span. I am using php codeigniter for the project , I don't know that whether the way i explained my problem is correct or not .
Yes you can so it with explode
if you have stored at least 2 words with space. try following
$demo ="demo text";
$arr = explode(" ",$demo);
$str = "<h2>".$arr[0]."<span>".$arr[1]."</span></h2>";
echo $str;
DEMO
EDIT
If you have more words and want to split first word only you can pass limit parameter in explode
$demo ="Pligrimage to Marian Shrines";
$arr = explode(" ",$demo,2);
$str = "<h2>".$arr[0]."<span>".$arr[1]."</span></h2>";
echo $str;
DEMO
I think you are looking for something like this!
$your_string = "Hello Houston! We have a problem!";
$my_array = explode(" ",trim($your_string));
$output = "<h2>";
foreach($my_array as $a_word){
if ($a_word === reset($my_array))
$output .= $a_word;
else
$output .= " <span>". $a_word . "</span>";
}
$output .= "</h2>";
print $output;
<?php
echo "I'm currently listening to</a> <a href='http://last.fm/artist/" . str_replace(" ","+",$artist) . "/_/" . str_replace(" ","+",$currenttrack) . "'>" . $currenttrack . "</a>";
?>
Above is my code. I'm trying to use str_replace() again on $artist and $currenttrack like:
str_replace("'","%27",$artist) and str_replace("'","%27",$currenttrack)
because the apostrophe doesn't go through correctly and messes with my code, but when I use it first with the spaces, it's already passed and won't change again.
What can I do?
If you want to do multiple replacements on the same string, you can pass arrays to str_replace:
str_replace(array(" ", "'"), array("+", "%27"), $artist)
However, when creating URL parameters, you shouldn't do the replacements yourself. You should use urlencode, and it will do all the necessary encodings.
Try this. It also makes your code more readable. Also, your anchor tags aren't formatted correctly.
$artist = str_replace(' ', '+', $artist);
$track = str_replace(' ', '%27', $currenttrack);
echo 'I\'m currently listening to ' . $track . '';
If I understood your question correctly, you are trying to replace spaces by + and ' by %27 in the two strings. To achieve this, you have to apply str_replace() on the result of the first operation. If $input is the original string, use:
$intermediate = str_replace(" ", "+", $input);
$result = str_replace("'", "%27", $intermediate);
There is a built in function to do the same you are trying to do: urlencode
$currenttrack = $artist = "X' xx WWW' w";
$url = "http://last.fm/artist/" . $artist . "/_/" . $currenttrack;
echo urlencode($url); // http%3A%2F%2Flast.fm%2Fartist%2FX%27+xx+WWW%27+w%2F_%2FX%27+xx+WWW%27+w
I got a file filter.txt with words that I want to highlight on a string.
Filter.txt:
test1
test2
My solution doesen't work:
<?php
$file = file_get_contents('/home/user/filter.txt', true);
$keyword=$file;
$str="This is a test1 and test2 and test3";
$keyword = implode('|',explode(' ',preg_quote($keyword)));
$str = preg_replace("/($keyword)/i","<b>$0</b>",$str);
echo $str;
?>
Anyone?
took me like 15 mins but I eventually got it :)
<?php
$file = file_get_contents('/home/user/filter.txt', true);
$keywords = $file;
$str = "This is a test1 and test2 and test3";
$keywords = explode(" ", $keywords);
$str = preg_replace('/'.implode('|', $keywords).'/', '<b>$0</b>', $str);
echo $str;
?>
and this is expecting your filter.txt to be laid out like test1 test2 test3 etc. I'd suggest separating by new line or a pipe, |
try trimming the contents you might be getting a whitespace after the second keyword
$keyword=trim( file_get_contents("filter.txt",true) );
I have a product database and I am displaying trying to display them as clean URLs, below is example product names:
PAUL MITCHELL FOAMING POMADE (150ml)
American Crew Classic Gents Pomade 85g
Tigi Catwalk Texturizing Pomade 50ml
What I need to do is display like below in the URL structure:
www.example.com/products/paul-mitchell-foaming-gel(150ml)
The problem I have is I want to do the following:
1. Remove anything inside parentheses (and the parentheses)
2. Remove any numbers next to g or ml e.g. 400ml, 10g etc...
I have been banging my head trying different string replaces but cant get it right, I would really appreciate some help.
Cheers
function makeFriendly($string)
{
$string = strtolower(trim($string));
$string = str_replace("'", '', $string);
$string = preg_replace('#[^a-z\-]+#', '_', $string);
$string = preg_replace('#_{2,}#', '_', $string);
$string = preg_replace('#_-_#', '-', $string);
return preg_replace('#(^_+|_+$)#D', '', $string);
}
this function helps you for cleaning url. (also cleans numbers)
try this,
<?php
$url = 'http%3A%2F%2Fdemo.com';
$decodedurl= urldecode($url);
echo $decodedurl;
?
$from = array('/\(|\)/','/\d+ml|\d+g/','/\s+/');
$to = array('','','-');
$sample = 'PAUL MITCHELL FOAMING POMADE (150ml)';
$sample = strtolower(trim(preg_replace($from,$to,$sample),'-'));
echo $sample; // prints paul-mitchell-foaming-pomade
Try this:
trim(preg_replace('/\s\s+/', ' ', preg_replace("/(?:\(.*?\)|\d+\s*(?:g|ml))/", "", $input)));
// "abc (def) 50g 500 ml 3m(ghi)" --> "abc 3m"