I'm trying to remove from a string variable, a character that appeared between spaces, I used some PHP functions to do it like str_replace but nothing happens.
An example below, I show it through var_dump PHP function :
string '�I�N�S�E�R�T� �[�d�b�o�]�
If anyone can tell me a way to do this, I'd be grateful.
That is a simple string translation operation (Demo):
$string = '�I�N�S�E�R�T� �[�d�b�o�]�';
echo strtr($string, array('�' => ''));
Output:
INSERT [dbo]
However you might have the problem to actually not knowing which character that is which then would require you to properly obtain the string first. So replacing it in the after would just be the wrong place.
You could try:
$str = '�I�N�S�E�R�T� �[�d�b�o�]�';
$filtered_str = filter_var($str, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH)
Demo. See: filter-var and filter.filters.sanitize
Related
I'm testing a preg_replace function, and I return from an ajax function the processed data (after I process the data through preg_replace, I put it through htmlentities() ):
My test string is:
pr eg123 ~!##$%^&*()-+={}|[]:;< >? "...,'/...
I'm trying to make sure all those characters aren't replaced. My replace function is:
$string = preg_replace('/[^a-zA-Z0-9\s+\n\r,.\/~!##\$%\^&*()\+={}\[\]|:;<>?\'"-]/', '', $string);
I return both the data from "echo" and after going through htmlentities() to see the difference.
when I return the data using alert(data), I get:
pr eg123 ~!##$%^&*()-+={}|[]:;< >? "...,'/...
pr eg123 ~!##$%^&*()-+={}|[]:;< >? "...,'/...
respectively. However, when I put either of those into $("#div").html(data), I get:
pr eg123 ~!##$%^&*()-+={}|[]:;< >? "...,'/...
so the multiple spaces are lost. Why does the .html() function reduce the spaces? And how can I fix this? Thanks
remove "\s+" from your regular expression and try again
"I'm trying to make sure all those characters aren't replaced." you mean it?
so i make a test like below:
$string = "~!##$%^&*()-+={}|[]:;< >?";
// $string = preg_replace('/[^a-zA-Z0-9]/', '', $string);
echo "'", $string, "'";
output is
'~!##$%^&*()-+={}|[]:;< >?'
if you want keep whole white space between "<" and ">" in $string, i can say that no way, if you wanna output the same white spaces as you input: there are two way can make this:
1> use <pre> tage
2> use replace the white space
does you want these? if you want to keep all? why use regular?
For some reason when preg_replace sees ¬ in string and replaces it with ¬:
$url= "http://something?blah=2&you=3&rate=22¬hing=1";
echo preg_replace("/&rate=[0-9]*/", "", $url) . "<br/>";
But the output is as follows:
http://something?blah=2&you=3¬hing=1 // Current result
http://something?blah=2&you=3¬hing=1 // Expected result
Any ideas why this is happening and how to prevent it?
& has special meaning when used URIs. Your URI contains ¬, which is a valid HTML entity on its own. It's being converted to ¬, hence causing the trouble. Escape them properly as ¬ to avoid this problem. If your data is fetched from elsewhere, you can use htmlspecialchars() to do this automatically.
Use this & in place of this &
because your &no has special meaning
use this url :
http://something?blah=2&you=3&rate=22¬hing=1
and then do your replace accordingly
I have a string like <div><center>[content]</center></div>.
The "[content]" gets replaced by a php script that inserts a content from a database.
I've solved that problem by using the str_replace function. Now I want to modify my script like that: If I type [content] it should replace it. If I write [[content]] it shouldn't do anything. I thought about using regex but whatever I try -> It does not work.
If you don't understand me, just think back to c#, java,...: If I type \n, its a newline. If I type \n its "\n". I would be very thankfull if anyone could help.
Take a look at this example:
"aa[content]aa" -> aaMYCONTENTaa
"aa[[content]]aa" -> aa[[content]]aa
"aa[[[content]]]aa" -> aa[MYCONTENT]aa
Pure regex solution is possible, but you are heading on a dangerous path of building your own templating engine. There are several solutions available (smarty is the most popular one)
<?php
$str = preg_replace('([^\[]?)\[content\]([^\]]?)', '\1' . $yourcontent . '\2', $str);
?>
Explanation: [^\\[]? matches everything but the [ character, including nothing. This means [[content]] doesn't match.
EDIT:
the code will replace [content] with the $replace_content_with value unless it has [ before or ] after
$original = '<div><center>[content]</center></div>';
$replace_content_with = 'my new content';
$pattern = '/([^\[])\[content\]([^\]])/i';
$new_data = preg_replace($pattern, '\1'.$replace_content_with.'\2', $original);
echo $new_data; //print <div><center>my new content</center></div>
note that if your replacement content include \[SOME NUMBER] in it - it might not work as expected
Just replace '>[content]<' with '>whatever you want<'
I am working with this daily data feed. To my surprise, one the fields didn't look right after it was in MySQL. (I have no control over who provides the feed.)
So I did a mysqldump and discovered the zip code and the city for this record contained a non-printing char. It displayed it in 'vi' as this:
<200e>
I'm working in PHP and I parse this data and put it into the MySQL database. I have used the trim function on this, but that doesn't get rid of it. The problem is, if you do a query on a zipcode in the MySQL database, it doesn't find the record with the non-printing character.
I'd like the clean this up before it's put into the MySQL database.
What can I do in PHP? At first I thought regular expression to only allow a-z,A-Z, and 0-9, but that's not good for addresses. Addresses use periods, commas, hyphens and perhaps other things I'm not thinking of at the moment.
What's the best approach? I don't know what it's called to define it exactly other than printing characters should only be allowed. Is there another PHP function like trim that does this job? Or regular expression? If so, I'd like an example. Thanks!
I have looked into using the PHP function, and saw this posted at PHP.NET:
<?php
$a = "\tcafé\n";
//This will remove the tab and the line break
echo filter_var($a, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
//This will remove the é.
echo filter_var($a, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
?>
While using FILTER_FLAG_STRIP_HIGH does indeed strip out the <200e> I mentioned seen in 'vi', I'm concerned that it would strip out the letter's accent in a name such as André.
Maybe a regular expression is the solution?
You can use PHP filters: http://www.php.net/manual/en/function.filter-var.php
I would recommend on using the FILTER_SANITIZE_STRING filter, or anything that fits what you need.
I think you could use this little regex replace:
preg_replace( '/[^[:print:]]+/', '', $your_value);
It basically strip out all non-printing characters from $your_value
I tried this:
<?php
$string = "\tabcde éç ÉäÄéöÖüÜß.,!-\n";
$string = preg_replace('/[^a-z0-9\!\.\, \-éâëïüÿçêîôûéäöüß]/iu', '', $string);
print "[$string]";
It gave:
[abcde éç ÉäÄéöÖüÜß.,!-]
Add all the special characters, you need into the regexp.
If you work in English and do not need to support unicode characters, then allow just [\x20-\x7E]
...and remove all others:
$s = preg_replace('/[^\x20-\x7E]+/', '', $s);
After imploding an array:
$in_list = "'".implode("','",$array)."'";
$in_list content is :
'Robert','Emmanuel','José','Alexander'
Now when i try to replace the word José by another string,
str_replace("José","J",$in_list);
It doesn't get the new value, José is still there. Am i missing something? thanx in advance.
How exactly do you try to replace the string?
When trying it this way:
$in_list = str_replace("José","J",$in_list);
echo $in_list;
everything should work fine.
Remember, the function is returning a value. So it returns a new string.
This should work. It depends on your array.
$str = array('Robert','Emmanuel','José','Alexander');
$str = implode(",", $str);
print str_replace('José', 'J', $str);
I'm not sure what's going on, It seems to work for me. What version of PHP are you using?
$in_list = "'".implode("','", array('Robert', 'Emmanuel', 'José', 'Alexander'))."'";
$replaced = str_replace("José", "J", $in_list);
//prints 'Robert','Emmanuel','J','Alexander'
echo $replaced;
See: http://codepad.viper-7.com/24qutm
try $in_list = html_entity_decode((str_replace(htmlentities("José"),"J",htmlentities($in_list));
Have you tried on a word without accents? I would say you have a character set mismatch, for example 'José' in $in_list is in latin1 character set and your PHP source file in UTF8.
If this is the case, you should first convert either your PHP file or the variable to the character set you want to work with.
Spontaneous guess: Those two strings are not the same. I suppose one "José" is a string hardcoded in your source code and the other is received from the database or the browser or so. If the encoding of the two strings is not the same, PHP won't identify them as identical and not replace the character. Make sure your source code file is saved in the same encoding as the data you're working on, preferably both being UTF-8.
This worked for me, but it doesn't look like I'm doing anything notably different from you?
$array = array('Robert', 'Emmanuel', 'José', 'Alexander');
$in_list = "'".implode("','",$array)."'";
echo $in_list.PHP_EOL;
echo str_replace("José","J",$in_list).PHP_EOL;
Output:
'Robert','Emmanuel','José','Alexander'
'Robert','Emmanuel','J','Alexander'
Keep in mind that str_replace will not perform the replacement on $in_list itself, but rather return a string containing the replacement.
Hope this helps!