This question already has answers here:
How do I replace certain parts of my string?
(5 answers)
Closed 6 years ago.
I want to replace special character single quotes(') with space . In Java it is easy but i dont know how to do this in php.
<?php $data = str_replace("'", " ", string);
echo $data;
?>
to remove just single quote :
$FileName = str_replace("'", " ", string);
to remove other characters as well, use array :
$remove[] = "'";
$remove[] = '"';
$remove[] = "-"; // just as another example
$FileName = str_replace( $remove, " ", string);
You can use str_replace() string function in php.
Example : echo str_replace(" ' "," ","India' ");
Explanation : Here, in str_replace() function first argument you need to pass string which you want to replace. For second argument you need to pass string you want to replace with existing. And for third and last argument you need to pass your original string in which you need replace.
Related
This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 3 years ago.
I need someone who can make the following output which is a single string
[{"mobile":"XXX-XXX-XXXX","permaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}","tempaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}"}]1
to
{"mobile":"XXX-XXX-XXXX",
"permaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}",
"tempaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}"}
Help me removing first '[' and last ']1' in the above string. Thanks in advance
This should work for you.
$final_str = rtrim(ltrim($your_str, '['), ']1');
Try below code,
<?php
$str = '[{"mobile":"XXX-XXX-XXXX","permaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}","tempaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}"}]1';
$temp_str = preg_replace('/\[/',"",$str);
$new_str = str_replace("]1","",$temp_str);
echo $new_str;
?>
Output,
{"mobile":"XXX-XXX-XXXX","permaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}","tempaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}"}
This will remove first and last character from your string
$result = substr($string, 1, -2);
Here is the link if you want to explore more:
https://www.w3schools.com/php/func_string_substr.asp
Try ltrim() and rtrim()
$str = '[{"mobile":"XXX-XXX-XXXX","permaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}","tempaddress":"{\"country\":\"country\",\"state\":\"state\",\"city\":\"city\",\"street\":\"street\"}"}]1';
$getTrim = ltrim($str, '[');
$getTrim = rtrim($getTrim, ']1');
echo $getTrim;
OR
$getTrim = rtrim(ltrim($str, '['), ']1');
you can use str_replace or preg_replace replace your keyword with empty string.
This question already has answers here:
PHP explode the string, but treat words in quotes as a single word
(5 answers)
Closed 7 years ago.
I have a string like this |casio|watch|men| in an xml. I want to explode this and i need output like this : $brand = "casio"; $cat = "watch";.
i used these codes but it doesnt helped me
<?php
$string = '|casio|watch|men|';
$string = explode('-',$string);
var_dump($string);
echo $string[0];
echo $string[1];
?>
your trying to separate the string using '-' while your string doesnt contain any... in your case you should explode the using '|' example
$string = explode('|',$string);
This question already has answers here:
PHP: How can I explode a string by commas, but not wheres the commas are within quotes?
(2 answers)
Closed 8 years ago.
I'm trying to figure out how to add double quote between text which separates by a comma.
e.g. I have a string
$string = "starbucks, KFC, McDonalds";
I would like to convert it to
$string = '"starbucks", "KFC", "McDonalds"';
by passing $string to a function. Thanks!
EDIT: For some people who don't get it...
I ran this code
$result = mysql_query('SELECT * FROM test WHERE id= 1');
$result = mysql_fetch_array($result);
echo ' $result['testing']';
This returns the strings I mentioned above...
Firstly, make your string a proper string as what you've supplied isn't. (pointed out by that cutey Fred -ii-).
$string = 'starbucks, KFC, McDonalds';
$parts = explode(', ', $string);
As you can see the explode sets an array $parts with each name option. And the below foreach loops and adds your " around the names.
$d = array();
foreach ($parts as $name) {
$d[] = '"' . $name . '"';
}
$d Returns:
"starbucks", "KFC", "McDonalds"
probably not the quickest way of doing it, but does do as you requested.
As this.lau_ pointed out, its most definitely a duplicate.
And if you want a simple option, go with felipsmartins answer :-)
It should work like a charm:
$parts = split(', ', 'starbucks, KFC, McDonalds');
echo('"' . join('", "', $parts) . '"');
Note: As it has noticed in the comments (thanks, nodeffect), "split" function has been DEPRECATED as of PHP 5.3.0. Use "explode", instead.
Here is the basic function, without any checks (i.e. $arr should be an array in array_map and implode functions, $str should be a string, not an array in explode function):
function get_quoted_string($str) {
// Here you will get an array of words delimited by comma with space
$arr = explode (', ', $str);
// Wrapping each array element with quotes
$arr = array_map(function($x){ return '"'.$x.'"'; }, $arr);
// Returning string delimited by comma with space
return implode(', ', $arr);
}
Came in my mind a really nasty way to do it. explode() on comma, foreach value, value = '"' . $value . '"';, then run implode(), if you need it as a single value.
And you're sure that's not an array? Because that's weird.
But here's a way to do it, I suppose...
$string = "starbucks, KFC, McDonalds";
// Use str_replace to replace each comma with a comma surrounded by double-quotes.
// And then shove a double-quote on the beginning and end
// Remember to escape your double quotes...
$newstring = "\"".str_replace(", ", "\",\"", $string)."\"";
This question already has answers here:
Replace last occurrence of a string in a string
(15 answers)
Closed 9 years ago.
I need to remove the domain name from the end of a string. I have tried the following code:
$domainNAME="example.com";
$result="_xmpp-client._tcp.example.com..example.com"
$string = $result;
$string = str_replace(".".$domainNAME, " ", $string);
Here the result is "_xmpp-client._tcp.". But the result should be "_xmpp-client._tcp.example.com.".
I need to remove the domain name only from the end of string, if domain name exists anywhere in the string it should not be removed.How can I change the code for that?
Any help should be appreciated!
No need for fancy preg nor substr.. just use trim() function :)
will remove from both ends
echo trim($string,$domainNAME);
will remove domainName from end of string
echo rtrim($string,$domainNAME);
will remove domainName from beging of string
echo ltrim($string,$domainNAME);
Example
echo rtrim('demo.test.example.com',"example.com");
//#return demo.test
2nd method
if not.. then use the preg match :).
$new_str = preg_replace("/{$domainNAME}$/", '', $str);
this will replace $domainNAME from $str ONLY if its at end of $str ($ sign after the var means AT end of string.
You could use preg_replace and specify the end of string marker $:
$string = preg_replace("/" . $domainNAME . "$/", " ", $string);
$domainNAME="example.com";
$result="_xmpp-client._tcp.example.com..example.com";
$string = $result;
$string = substr($result,0, strrpos($result, $domainNAME)-1);
echo $string;
if you are truly wanting to have the output as _xmpp-client._tcp.example.com. with the dot at the end use
preg_replace("/\." . $domainNAME . "$/", " ", $string);
and you can add ? if you want it to be optional
preg_replace("/\.?" . $domainNAME . "$/", " ", $string);
Demo
$domainNAME="example.com";
$length = strlen(".".$domainNAME);
$result="_xmpp-client._tcp.example.com..example.com";
$string = substr($result, 0, $length*(-1));
Try that. I wish it could help you
Use str_ireplace('example.com.example.com', 'example.com', $string);
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
quick function to replace ' with \' in php
Is there a PHP function that only adds slashes to double quotes NOT single quotes
I have for example:
$one = 'put "returns" between "paragraphs"';
$two = '"linebreak" add 2 spaces "at end"';
How can I convert this for:
$one = 'put \"returns\" between \"paragraphs\"';
$two = '\"linebreak\" add 2 spaces \"at end\"';
$one = str_replace('"', '\"', $one);
To add the slashes, use
$one = addslashes($one);
Or to remove
$one = stripslashes($one);
The function you're looking for is either str_replace or addcslashes:
$one = 'put "returns" between "paragraphs"';
$slashed = addcslashes($one, '"');
echo $slashed;
Demo.