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bUic7ksoxFhlcfmsJGHDgakBQdAtaYOxSS96MvMLrl8OEMpKE9RtXITsXTj/JicJoSJbY+w3Mszce1vHs0YkdCZug6BRRxLM3Htbx7NGL91Op3oo9u0Jb8jdQb2jNB0MHk4j4XRiEuvf9jqO9829DB5OI+F0YhE8ZmQzVYvJYyVocMjBLRdf75AUVejP7gcKxceCT8ZcPQ9eUPjjCRynCsXHgk/GXDNMMfkG5pf9r+a+AV7EuUxD/rQwey6ePEV0FCHDgHtZK5KmPKJOYJlVl/JlDlxq+MSvmkp1o/5v4SsPAX5jO5KQ=="
I have tried the below method.
str_replace("\n", "", $key);
But it not worked.
Any suggestions..?
Thanks
TRY
$order = array("\r\n", "\n", "\r");
$replace = '';
$final = str_replace($order, $replace, $str);
var_dump($final);
NOTE : Because str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements. so Processes \r\n's first so they aren't converted twice.
Try this,I think it is a simple way
echo str_replace(PHP_EOL, '', $key);
Try this:
echo str_replace("\r\n","", $key);
Try Below Code
str_replace(chr(13), "", $key);
May this will help you
Related
My string:
'KCP-PRO;first_name last_name;address;zipcode;country' //for example: 'KCP-PRO;Jon Doe;Box 564;02201;USA'
or
'KCP-SAT-PRO;first_name last_name;address;zipcode;country'
How can i change the first part (KCP-PRO or KCP-SAT-PRO) and change it to (KCP,PRO or KCP,SAT,PRO)? The outcome has to be:
'KCP,PRO;first_name last_name;address;zipcode;country'
or
'KCP,SAT,PRO;first_name last_name;address;zipcode;country'
I haven't tried the code myself but I guess this will do the trick
$string = 'KCP-SAT-PRO;first_name last_name;address;zipcode;country';
$stringExploded = explode(';', $string);
$stringExploded[0] = str_replace('-', ',', $stringExploded[0]);
$output = implode(';', $stringExploded);
//output should be KCP,SAT,PRO;first_name last_name;address;zipcode;country
Hope this helps :)
Or you can use preg_replace_callback function with the following regex
^[^;]*
So your code looks like as
echo preg_replace_callback("/^[^;]*/",function($m){
return str_replace("-",',',$m[0]);
},"KCP-SAT-PRO;first_name last_name;address;zipcode;country");
I've tried:
$input = str_replace('[IMG]',"",$input,$rplc);
with no success.
I've also tried escaping and double-escaping [IMG] by doing '[IMG]' and '\[IMG\]', neither worked. It doesn't throw any errors, but it doesn't actually replace anything either.
How can I get this to work?
Have you tried something like:
$input = preg_replace("/(\\[IMG\\])/", $replacement, $input);
Just try this example, to implement within your code..
$search = "[IMG]";
$replace = "";
$string = "Hello [IMG] Approxx? How[IMG] are [IMG]You?";
$input = str_replace($search, $replace, $string);
echo $input;
Try this
preg_replace("#\[img\]#","",$input);
And the same result can also be achived by using
str_replace()
str_replace("[img]",'',$input);
This question already has answers here:
Add quotation marks to comma delimited string in PHP
(5 answers)
Closed 1 year ago.
I have a variable with string value of 'laptop,Bag' and I want it to look like ' "laptop","Bag" 'or "laptop","Bag". How could I do this one? Is there any php function that could get this job done? Any help please.
This would work. It first, explodes the string into an array. And then implodes it with speech marks & finishes up by adding the opening & closing speech mark.
$string = "laptop,bag";
$explode = explode(",", $string);
$implode = '"'.implode('","', $explode).'"';
echo $implode;
Output:
"laptop","bag"
That's what str_replace is for:
$result = '"'.str_replace(',', '","', $str).'"';
This would be very easy to do.
$string = 'laptop,bag';
$items = explode(',', $string);
$newString = '"'.implode('","', $items).'"';
That should turn 'laptop,bag' into "laptop","bag".
Wrapping that in a function would be as simple as this:
function changeString($string) {
$items = explode(',', $string);
$newString = '"'.implode('","', $items).'"';
return $newString;
}
I think you can explode your string as array and loop throw it creating your new string
function create_string($string)
{
$string_array = explode(",", $string);
$new_string = '';
foreach($string_array as $str)
{
$new_string .= '"'.$str.'",';
}
$new_string = substr($new_string,-1);
return $new_string;
}
Now you simply pass your string the function
$string = 'laptop,Bag';
echo create_string($string);
//output "laptop","Bag"
For your specific example, this code would do the trick:
<?php
$string = 'laptop,bag';
$new_string = ' "' . str_replace(',', '","', $string) . '" ';
// $new_string: "laptop","bag"
?>
That code would also work if you had more items in that list, as long as they are comma-separated.
Use preg_replace():
$input_lines="laptop,bag";
echo preg_replace("/(\w+)/", '"$1"', $input_lines);
Output:
'"laptop","Bag"'
I think you can perform that using explode in php converting that string in to an array.
$tags = "laptop,bag";
$tagsArray = explode(",", $tags);
echo $tagsArray[0]; // laptop
echo $tagsArray[1]; // bag
Reference
http://us2.php.net/manual/en/function.explode.php
related post take a look maybe could solve your problem.
How can I split a comma delimited string into an array in PHP?
I've got this string, but I need to remove specific things out of it...
Original String: hr-165-34.sh-290-92.ch-215-84.hd-180-1.lg-280-64.
The string I need: sh-290-92.ch-215-84.lg-280-64.
I need to remove hr-165-34. and hd-180-1.
!
EDIT: Ahh ive hit a snag!
the string always changes, so the bits i need to remove like "hr-165-34." always change, it will always be "hr-SOMETHING-SOMETHING."
So the methods im using wont work!
Thanks
Depends on why you want to remove exactly those Substrigs...
If you always want to remove exactly those substrings, you can use str_replace
If you always want to remove the characters at the same position, you can use substr
If you always want to remove substrings between two dots, that match certain criteria, you can use preg_replace
$str = 'hr-165-34.sh-290-92.ch-215-84.hd-180-1.lg-280-64';
$new_str = str_replace(array('hr-165-34.', 'hd-180-1.'), '', $str);
Info on str_replace.
The easiest and quickest way of doing this is to use str_replace
$ostr = "hr-165-34.sh-290-92.ch-215-84.hd-180-1.lg-280-64";
$nstr = str_replace("hr-165-34.","",$ostr);
$nstr = str_replace("hd-180-1.","",$nstr);
<?php
$string = 'hr-165-34.sh-290-92.ch-215-84.hd-180-1.lg-280-64';
// define all strings to delete is easier by using an array
$delete_substrings = array('hr-165-34.', 'hd-180-1.');
$string = str_replace($delete_substrings, '', $string);
assert('$string == "sh-290-92.ch-215-84.lg-280-64" /* Expected result: string = "sh-290-92.ch-215-84.lg-280-64" */');
?>
Ive figured it out!
$figure = $q['figure']; // hr-165-34.sh-290-92.ch-215-84.hd-180-1.lg-280-64
$s = $figure;
$matches = array();
$t = preg_match('/hr(.*?)\./s', $s, $matches);
$s = $figure;
$matches2 = array();
$t = preg_match('/hd(.*?)\./s', $s, $matches2);
$s = $figure;
$matches3 = array();
$t = preg_match('/ea(.*?)\./s', $s, $matches3);
$str = $figure;
$new_str = str_replace(array($matches[0], $matches2[0], $matches3[0]), '', $str);
echo($new_str);
Thanks guys!
I have this string:
"small:
http://img.exent.com/free/frg/products/666550/player_boxshot.jpg
boxshot:
http://img.exent.com/free/frg/products/666550/boxshot.jpg"
I want to get only the first image link (after the word "small") and ignore all the rest (the word "boxshot" and the link after it).
How can I make it?
use it and like my ans. string is your above whole string
substr(string,0,stripos(string,"boxshot")-1);
$string = "small:
http://img.exent.com/free/frg/products/666550/player_boxshot.jpg
boxshot:
http://img.exent.com/free/frg/products/666550/boxshot.jpg";
preg_match_all('~http(.*?)jpg~i',trim($string),$matches);
var_dump($matches[0][0]);
The easy way:
<?php
$str='small: http://img.exent.com/free/frg/products/666550/player_boxshot.jpg boxshot: http://img.exent.com/free/frg/products/666550/boxshot.jpg';
$parts=explode('boxshot:',$str);
$small=$parts[0];
echo($small);
?>
Try this:
$string = "small:
http://img.exent.com/free/frg/products/666550/player_boxshot.jpg
boxshot:
http://img.exent.com/free/frg/products/666550/boxshot.jpg";
// replace all 2 or more consecutive spaces with 1 space
$string = preg_replace( '/\s\s+/i', ' ', $string);
$array = explode( ' ', $string);
echo $array[1];
Hope this helps.