I am trying to match a whole UTF-8 word in PHP. This is how I am trying to do it:
<?php
$string = 'DS DAMAT TAKIM ELBİSE (GOLD)';
$search = 'takım elbise';
$replace = 'TakımElbise';
$result = mb_eregi_replace('/\b'.$search.'\b/ui', $replace, $string);
echo $result;
echo preg_match('/\b'.$search.'\b/ui', $replace);
?>
But it does not work. What can be the problem?
NOTE:
I have tried adding these lines at the beginning of script:
mb_internal_encoding('UTF-8');
mb_regex_encoding('UTF-8');
No result.
How about:
$string = 'DS DAMAT TAKIM ELBİSE (GOLD)';
// ^__ this isn't an I
$search = 'takım elbİse';
// ^__ this isn't an I
$replace = 'TakımElbise';
$result = preg_replace("/\b$search\b/ui", $replace, $string);
echo $result;
I've just change the i to İ in the search string. You may want to use lowercase (I haven't on my keyboard)
See the comment here: http://php.net/manual/en/function.mb-ereg-replace.php
Unlike preg_replace, mb_ereg_replace doesn't use separators
Example with preg_replace:
$data = preg_replace("/[^A-Za-z0-9\.\-]/","",$data);
Example with mb_ereg_replace:
$data = mb_ereg_replace("[^A-Za-z0-9\.\-]","",$data);
Also, don't use the ui flags.
Related
I have a strange problem with a simple preg_replace, if I use
$pattern = '/<div class="formula">(.*?)<\/div>/';
$str = preg_replace($pattern, "", $str);
not work correctly, nothing is replaced....if I put a static string instead of $str all work correctly.
string(2133) "Velocità: <div class="formulaTex">...</div><div class="formula">...</div>
why?there is some kind of encoding to use?
if I pass the var to preg_replace not work, if I pass the static string it work!
this is my code:
$db = new PDO('sqlite:ARGOMENTI_NEW.sqlite');;
$result = $db->query("SELECT * FROM Testi WHERE IDSezione = 100");
while($row = $result->fetchAll(PDO::FETCH_ASSOC)){
$pattern = '/<div class="formula">(.*?)<\/div>/';
$str = preg_replace($pattern, "", $row[0]["Testo"]);
echo $str . "<br/><br/><br/>";
}
thanks
I find the error, the string from sql query stamp encoded html entity, on the screen I see the correct string but real string have different characters....this is the problem! whit this I solve:
$str = preg_replace( "/\r|\n/", "", html_entity_decode($row[0]["Testo"]));
How do I make multiple preg_match on a string. I did a research and able to come out with below solutions.
<?php
$input = '#Hello# & Good Day ~World~';
$regex = '/
~
(.*?)
~
/six';
$input_new = preg_replace($regex,'<i>$1</i>', $input);
echo $input_new;
the above will search for (~)string(~) and change to italic format. How do I want to make the search for (#)string(#) and change to bold format on the same text.
preg_replace, as the manual says, can also take more than one pattern and replacement:
<?php
$input = '#Hello# & Good Day ~World~';
$regexes = array('/~(.*?)~/six',
'/#(.*?)#/six'
);
$replaces = array('<i>$1</i>',
'<b>$1</b>'
);
$input_new = preg_replace($regexes, $replaces, $input);
echo $input_new;
You do the same thing you did above, only this Time changing to and to like so. Otherwise, just create a Function to do that like so:
<?php
function transposeBoldItalic($inputString, $embolden="Hello",$italicise="World"){
$result = preg_replace("#(" . preg_quote($embolden) . ")#", "<strong>$1</strong>", $inputString);
$result = preg_replace("#(" . preg_quote($italicise) . ")#", "<em>$1</em>", $result);
return $result;
}
// TEST IT:
$inputString = "Hello & Good Day World";
var_dump(transposeBoldItalic($inputString, "Hello", "World"));
echo(transposeBoldItalic($inputString, "Hello", "World"));
// DUMPS
<strong>Hello</strong> & Good Day <em>World</em>
Test it Here: https://eval.in/571784
#osnapitzkindle answer is correct, but you can also use preg_replace_callback
echo preg_replace_callback('/([#~])(.*?)([#~])/', function ($matches){
return (strpos($matches[1], '#') !== false) ? "<i>{$matches[2]}</i>" : "<b>{$matches[2]}</b>";}, $input
);
Ideone Demo
Thanks to #s.d.a.p.e I've come a step close but I'm not quite there yet.
What I'm trying to do is replace all instances of a string in a block of text. I want to replace something like this:
user is ?user_id=34&first_name=Ralph so is ?user_id=1 also
With this:
user is /user/34/ so is /user/1/ also
Here is the preg_replace code I'm using:
$pattern = '#\?user_id=([0-9]+)#';
$replace = '/user/$1/';
echo preg_replace($pattern,$replace,$string);
With that pattern I end up with this:
user is /user/34/&first_name=Ralph so is /user/1/ also
Thanks again.
try this:
$string = "user is ?user_id=34&first_name=Ralph so is ?user_id=1 also";
$result = preg_replace('/\?(user)_id=(\d+)(.*?)(?! )/i', '/$1/$2/$3', $string );
echo $result ;
Output:
user is /user/34/&first_name=Ralph so is /user/1/ also
DEMO
I'd use this:
$string = 'user is ?user_id=34&first_name=Ralph so is ?user_id=1 also';
$pattern = '#\?user_id=([0-9]+)\S*#';
$replace = '/user/$1/';
echo preg_replace($pattern, $replace, $string);
Where \S stands for any character that is not a space.
Output:
user is /user/34/ so is /user/1/ also
print preg_replace(
'#\?user_id=([0-9]+)\&(first_name=(?:.*))#',
'/user/$1?$2',
'?user_id=34&first_name=Ralph'
);
result :
/user/34?first_name=Ralph if get it right..
I want to ignore a specific character using php. So when a user adds this character in the textbox. the php scripts filters it out first. I tried something and came up with this:
<?php
$datetogoto = $_GET['datetogoto'];
$pattern = '-';
$replace = '';
preg_replace($pattern, $replace, $datetogoto);
header('Location: ../index.php?newsdate='.$datetogoto);
?>
So what it wrong with this code?
Can you try using str_replace
$datetogoto = $_GET['datetogoto'];
$datetogoto = str_replace("-","", $datetogoto);
Ref: http://us1.php.net/str_replace
Or , if you want get date format whatever you sent in query string, then use urlencode()
header('Location: ../index.php?newsdate='.urlencode($datetogoto));
PHP regex needs delimiters, so use it like this:
$pattern = '/-/';
OR else use str_replace:
str_replace('-', $replace, $datetogoto);
I want to get all Performance ID's from this page .
<?php
$content = file_get_contents("http://www124.popmundo.com/Common/Performances.asp?action=ComingPerformances&ArtistID=1962457");
$regex = "Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)";
//$regex = "/Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)/";
//$regex = "/Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)/s";
//all pattern variations tested, not working
if(preg_match_all($regex, $content, $m))
print_r($m);
else
echo "FALSE";
// this is returning FALSE
Use & instead of & in your regex.
Try this:
$regex = "/Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)/";
It looks like an escape problem. Not knowing php, I would guess one of these
might fix it:
$regex = 'Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)';
or
$regex = "Performances\\.asp\\?action=Arrangements&PerformanceID=([0-9]+)";
or
$regex = '/Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)/';