This question already has answers here:
How do I replace certain parts of my string?
(5 answers)
Closed last month.
Is there a anyway to remove apostrophe from a string in php?
example:- If string is Mc'win then it should be shown as Mcwin
$Str = "Mc'win";
/*
some code to remove the apostrophe
*/
echo $Str; // should display Mcwin
You can use str_replace.
$Str = str_replace('\'', '', $Str);
or
$Str = str_replace("'", '', $Str);
This will replace all apostrophes with nothing (the 2nd argument) from $Str. The first example escapes the apostrophe so str_replace will recognize it as the character to replace and not part of the enclosure.
If your variable has been sanitized, you may be frustrated to find you can't remove the apostrophe using $string = str_replace("'","",$string);
$string = "A'bcde";
$string = filter_var($string, FILTER_SANITIZE_STRING);
echo $string." = string after sanitizing (looks the same as origin)<br>";
$string = str_replace("'","",$string);
echo $string." ... that's odd, still has the apostrophe!<br>";
This is because sanitizing converts the apostrophe to ' , but you may not notice this, because if you echo the string it looks the same as the origin string.
You need to modify your replace search characters to '
which works after sanitizing.
$string = str_replace("'","",$string);
in my case, i got single quote issue when i wanted to store it to database (in my case MySQL). So, i remove the single quotes using this method
str_replace("'", "", trim($_GET["message"]))
But, the problems comes. Some data required us to have single quotes. So, instead of removing the quotes I try to save the single quotes (escaping single quotes) so it can be used in the future (in my case at Android)
My Idea is to replace from ' to ''.
So here is the final
$content = str_replace("'", "''", trim($_GET["message"])); // double quotes for escape single quotes
This answer is for someone that persist problem like me. I got better solution. Cheers!
Related
I want to add a backslash "\" before all non alphanumeric characters like "how are you \:\)", so I used this:
$code = preg_replace('/([^A-Za-z0-9])/i', '\$1', $code);
But it doesn't work. Instead it just echos '\$1'. What am I doing wrong?
I also tried
$code = preg_replace('/([^A-Za-z0-9])/i', '\\$1', $code);
But won't work.
You need four backslashes:
$code = preg_replace('/([^A-Za-z0-9])/i', '\\\\$1', $code);
The reason is that the backslash escapes itself in PHP string context (even single quotes). For PCRE to see even one, you need at least two. But to not being misinterpreted to mask the replacement placeholder, you need to double that still. (Btw, three backslashes would also accidentially work.)
EXAMPLE:
<?php
$str = "Is your name O'reilly?";
// Outputs: Is your name O\'reilly?
echo addslashes($str);
?>
I have a series of strings that I have read in with fgets() from a .csv file, and then exploded into an array. The file has the csv standard double quotes. For example:
video
is one of the values that I am reading in. I need all instances of "" to be replaced by just ". I know that this is a simple solution, but I can't seem to wrap my head around the way that the escape characters would work.
Use str_replace:
$string = 'test';
$string = str_replace('""', '"', $string);
echo $string;
// Outputs: test
I have a PHP page which gets text from an outside source wrapped in quotation marks. How do I strip them off?
For example:
input: "This is a text"
output: This is a text
Please answer with full PHP coding rather than just the regex...
This will work quite nicely unless you have strings with multiple quotes like """hello""" as input and you want to preserve all but the outermost "'s:
$output = trim($input, '"');
trim strips all of certain characters from the beginning and end of a string in the charlist that is passed in as a second argument (in this case just "). If you don't pass in a second argument it trims whitespace.
If the situation of multiple leading and ending quotes is an issue you can use:
$output = preg_replace('/^"|"$/', '', $input);
Which replaces only one leading or trailing quote with the empty string, such that:
""This is a text"" becomes "This is a text"
$output = str_replace('"', '', $input);
Of course, this will remove all quotation marks, even from inside the strings. Is this what you want? How many strings like this are there?
The question was on how to do it with a regex (maybe for curiosity/learning purposes).
This is how you would do that in php:
$result = preg_replace('/(")(.*?)(")/i', '$2', $subject);
Hope this helps,
Buckley
I have a string as below
$str = '"Mark Zuckerberg" facebook "A social utility connecting friends" profile';
I want it to be manipulated as follows
$output = '"Mark Zuckerberg" OR facebook OR "A social utility connecting friends" OR profile';
What I am trying to have in output is all the units combined with OR in between them. Here a unit is wither a single word when its not in double quotes or the complete string that falls within the single quotes.
I wanted to try with preg_replace. But am unable to get a correct regular expression to match. Kindly help!
$result = preg_replace('/ (?=(?:[^"]*"[^"]*")*[^"]*$)/', ' OR ', $subject);
works if you don't have any escaped quotes in your string. It replaces spaces only if they are followed by an even number of double quotes.
Any reason why preg_replace rather than a simple
$output = '"'.implode('" OR "',str_getcsv($str,' ')).'"';
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
URL Friendly Username in PHP?
im somehow confused in using proper functions to escape and create a slug
i used this :
$slug_title = mysql_real_escape_string()($mtitle);
but someone told me not to use it and use urlencode()
which one is better for slugs and security
as i can see in SO , it inserts - between words :
https://stackoverflow.com/questions/941270/validating-a-slug-in-django
Using either MySQL or URL escaping is not the way to go.
Here is an article that does it better:
function toSlug($string,$space="-") {
if (function_exists('iconv')) {
$string = #iconv('UTF-8', 'ASCII//TRANSLIT', $string);
}
$string = preg_replace("/[^a-zA-Z0-9 -]/", "", $string);
$string = strtolower($string);
$string = str_replace(" ", $space, $string);
return $string;
}
This also works correctly for accented characters.
mysql_real_escape_string() has different purpose than urlencode() which both aren't appropriate for creating a slug.
A slug is supposed to be a clear & meaningful phrase that concisely describes the page.
mysql_real_escape_string() escapes dangerous characters that can change the purpose of the original query string.
urlencode() escapes invalid URL characters with "%" followed by 2 hex digits that represents their code (e.g. %20 for space). This way, the resulting string will not be clear & meaningful, because of the unpleasant characters sequences, e.g. http://www.domain.com/bad%20slug%20here%20%3C--
Thus any characters which may be affected by urlencode() should be omitted, except for spaces that are usually replaced with -.