I tried using both lines below:
preg_replace("/[^A-Za-z0-9 ]/", '', $string);
preg_replace("/[^[:alnum:][:space:]]/u", '', $string);
But, if $string has a single quote, it is replaced by "039" and I don't understand why:
don't
becomes
don039t
In don't Instead of single quote you are using '. In html view it display as single code. While replacing(preg_replace) spacial characters will remove and 039(from ') only remaining.
As already mentioned, your string is encoded with htmlentities.
Try:
preg_replace("/[^A-Za-z0-9 ]/", '', html_entity_decode($string, ENT_QUOTES));
See this example
Specify ENT_QUOTES to make it deal with quotes.
If you are still looking for a way to remove characters, without decoding your entities, you can try
preg_replace("/[^a-z0-9& ]|&(#[0-9]{2,3})?/i", '', $string);
Try with:
$exampleString = "Hi! Jo libertaire. This is working fine \"Yes absolutely fine\".Don't say its not working. you can try this string. :)";
$result= preg_replace("/[^A-Za-z0-9 ]/", '', $exampleString);
print $result;
I don't have a server for php so I tried this in an online editor and it's working fine.
Related
$string = #iconv("UTF-8", "UTF-8", $string);
I'm using this code to replace Unicode characters in my string, but actually what this does is remove all characters after the first Unicode sign in the string. Is there any other function to helps me to do this?
I suggest doing this with preg_replace like this:
preg_replace('/[\x00-\x1F\x7F]/u', '', $string);
or even better:
preg_replace('/[\x00-\x1F\x7F\xA0]/u', '', $string);
If the above does not work for your case, this might:
preg_replace( '/[^[:cntrl:]]/', '',$string);
There is also the option to filter what you need instead of removing what you do not. Something like this should work:
filter_var($string, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH);
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 need to replace everything in a string that is not a word,space,comma,period,question mark,exclamation mark,asterisk or '. I'm trying to do it using preg_replace, but not getting the correct results:
$string = "i don't know if i can do this,.?!*!##$%^&()_+123|";
preg_replace("~(?![\w\s]+|[\,\.\?\!\*]+|'|)~", "", $string);
echo $string;
Result:
i don't know if i can do this,.?!!*##$%^&()_+123|
Need Result:
i don't know if i can do this,.?!*
I don't know if you're happy to call html_entity_decode first to convert that ' into an apostrophe. If you are, then probably the simplest way to achieve this is
// Convert HTML entities to characters
$string = html_entity_decode($string, ENT_QUOTES);
// Remove characters other than the specified list.
$string = preg_replace("~[^\w\s,.?!*']+~", "", $string);
// Convert characters back to HTML entities. This will convert the ' back to '
$string = htmlspecialchars($string, ENT_QUOTES);
If not, then you'll need to use some negative assertions to remove & when not followed by #, ; when not preceded by ', and so on.
$string = preg_replace("~[^\w\s,.?!*'&#;]+|&(?!#)|&#(?!039;)|(?<!&)#|(?<!');~", "", $string);
The results are subtly different. The first block of code, when provided ", will convert it to " and then remove it from the string. The second block will remove & and ; and leave quot behind in the result.
To get a double quoted string (which I cannot change) correctly parsed I have to do following:
$string = '15 Rose Avenue\n Irlam\n Manchester';
$string = str_replace('\n', "\n", $string);
print nl2br($string); // demonstrates that the \n's are now linebreak characters
So far, so good.
But in my given string there are characters like \xC3\xA4. There are many characters like this (beginning with \x..)
How can I get them correctly parsed as shown above with the linebreak?
You can use
$str = stripcslashes($str);
You can escape a \ in single quotes:
$string = str_replace('\\n', "\n", $string);
But you're going to have a lot of potential replaces if you need to do \\xC3, etc.... best use a preg_replace_callback() with a function(callback) to translate them to bytes
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'=>'', ' '=>''));