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
Related
I'm new to PHP and I have a problem.
I need delete all chars since a symbol (Sorry for my bad english , i'm from argentina)
I have this text :
3,94€
And I need the text is as follows:
3,94
I tried this by multiple ways but it didn't work.
There are a few ways you can do this:
Using strpos:
$string = '3,94€';
echo substr($string, 0, strpos($string, '&'));
or using strstr:
// Requires PHP 5.3+ due to the true (before_needle) parameter
$string = '3,94€';
echo strstr($string, '&', true);
or using explode:
// Useful if you need to keep the € part for later
$string = '3,94€';
list($part_a, $part_b) = explode('&', $string);
echo $part_a;
or using reset:
$string = '3,94€';
echo reset(explode('&', $string));
The best suited in your case would be to use strpos to find the first occurrence of & in the string, and then use substr to return the string from the begining until the value returned by strpos.
Another posibility is clean the number and then round it:
<?php
//Option 1: with regular expresions:
$val = '3,94€';
$res = preg_replace('/[^0-9.,]/','',$val);
var_dump($res);
//Option 2: with filter functions:
$res2 = filter_var($val, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND);
var_dump($res2);
//Output: 3,948364
//If you want to round it:
$res = substr($res, 0, 4);
var_dump($res);
?>
You can use regex: https://regex101.com/r/uY0kH3/1
It will work in preg_match() function.
You could use str_replace.
<?php
$string = 3,94€
$final = str_replace('€' ,'', $string);
echo $final;
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 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.
I am writing a program in PHP, and i need to find data that is in between two sets of symbols, and convert that to a string. For example
$main = "Hello, everyone, my name is (-Jack-)"
$string = regex_function('(-', $main) #should return "Jack"
How do i get that output, using a regex function or something
Try this :
$main = 'Hello, everyone, my name is (-Jack-)';
preg_match_all('/\(\-(?P<name>.*)\-\)/', $main, $matches);
echo "<pre>";
print_r($matches);
echo $matches['name'][0];
The function is known as preg_match_all().
$main = "Hello, everyone, my name is (-Jack-)";
preg_match_all('/\(\-(?P<name>\w+)\-\)/', $main, $string);
print_r( $string );
A sample output on codepad.
Referring to #Prasanth's comment; here's a better regex.
$main = "Hello, everyone, my name is (-Jack stuff-) some more text (-John stuff-)";
preg_match_all('/\(\-(?P<name>[\s\w]+)\-\)/', $main, $string);
print_r( $string );
Codepad link.
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]+)/';