I got a string in which I replace all occurrences of [CODE]...[/CODE]. With preg_replace_callback can I call a function which handles the content of those tags. But how can I manipulate all string which are around those occurrences?
Example:
$str = "Hello, I am a string with [CODE]some code[/CODE] in it";
Now, with preg_replace_callback I manipulate the content of [CODE], in this case some code. But I'd like for all other text in this string, so Hello, I am a string with and in it to do something different. How could I do this the best way?
Thank you for you help!
Flo
It'd be simpler if I could see the regex, but the gist is that I think you want capture groups.
You should be able to access those regions separately by placing them into parenthesis-wrapped groups. Each section will be available to your callback. So (crudely) something like /(.*)(\[CODE\].*\[/CODE\])(.*)/ should pass an array of matches to your callback
Related
I need to parse the id from the following string:
https://itunes.apple.com/us/album/24k-magic/id1161503945?i=1161504024&uo=2
I need to only return the following:
id1161503945
The string always begins with https://itunes.apple.com/ and ends with ?i=#####&uo=2
I tried string and replace with wildcards but that did not work.
Well, you can use this below regex. It is working. I have use preg_replace function.
$data = 'https://itunes.apple.com/us/album/24k-magic/id1161503945?i=1161504024&uo=2';
echo preg_replace("/(.*)\/(\w+)\?(.*)/","$2",$data);
Output is
id1161503945
Or You can use
preg_match("/(\/)(\w+)(\?)/",$data,$m);
echo $m[2];
Same output.
Hope it help you
If it's really always the last element (before query params) in the url, then you can use this simple regex:
'/id[^?]+/'
CAUTION: as pointed by #xhienne, this works only if you're sure that another id string doesn't appear anywhere before the searched part.
If it may happen, rather use:
'/id[\d]+/'
This way, it's safe with respect to a previous id string, but the searched id must be followed by digits only.
I have the following string inside the source of some website:
user_count: <b>5.122.512</b>
Is this possible to get the number out of this string, even if the tags around this number were different? I mean, "user_count:" part won't change, but the tags can be changed, to strong for example. Or the tags could be doubled, or whatever.
How can I do that?
You can use
user_count:\s*<.*?>(.*?)<.*?>
See DEMO
I'd imagine you have to use JS to extract the content between the tags <b>5.122.512<b> from the DOM.
If you can assign an ID to this you can probably use document.getElementById('NAME_OF_YOUR_ID').innerHTML; to extract the number between it. If you need to process this inside a PHP script, you would probably need to POST this back to the server.
There are a couple of ways to get the number out of the string. One would be just to strip the tags and run a regular expression.
$s = "user_count: <b>5.122.512</b>"
preg_match_all("#user_count: (.+)#", strip_tags($s), $matches);
print_r($matches)
$matches[1] should match the number.
I'm making a function call to a library that is returning a malformed json array. I can work around this if I can get a preg written to extract the part that I want.
The array is a jumbled mess, but buried deep inside it is a string that looks like this:
token=??????,
I need to write a preg to grab the characters represented by the question marks. I wrote this, but it's not getting the part of the text that I want:
$token = preg_match('#^(?:token=)?([^,]+)#i', $badJson, $matches);
Can anyone help me? Thanks.
You can try:
/token=([^,]+)/i
and the use the first sub-match to extract the token. Being more specific is usually a good idea with regex (eg. does the token have a set length? does it only contain hex characters? etc.)
Site note: https://leaverou.github.io/regexplained/ is a great site for testing regular expressions.
I am trying to work out the optimal way to replace all PHP variables within a string of code with a call to an array instead as shown below.
E.g. source code string
$random_var_name + $random_var_name2 * $diff_var_name3
Transformed into
$varArray["random_var_name"] + $varArray["random_var_name2"] * $varArray["diff_var_name3"]
I had thought that preg_replace() was the optimal solution, but the difficulty comes with the need to perform the replacement with a sub-part of the search pattern.
Perhaps it is better to just retrieve all the variables with a preg_match, edit/wrap them, then perform a single str_replace() for each variable?
However this is probably considerably slower.
The following regex should do what you're asking:
preg_replace('/\$([a-zA-Z_0-9]+)/', '$varArray["$1"]', $input_string);
In order to avoid to change $var['foo'] to $varArray["var"]['foo'] you have to check there're no [ character after the variable name. For this use a negative look-ahead:
$string = preg_replace('/\$(\w+)(?![\w\[])/', '$varArray["$1"]', $string);
Ok, so here's my issue:
I have a link, say: http://www.blablabla.com/watch?v=1lyu1KKwC74&feature=list_other&playnext=1&list=AL94UKMTqg-9CfMhPFKXPXcvJ_j65v7UuV
And the link is between two tags say like this:
<br>http://www.blablabla.com/watch?v=1lyu1KKwC74&feature=list_other&playnext=1&list=AL94UKMTqg-9CfMhPFKXPXcvJ_j65v7UuV<br></p>
Using this regex with preg_replace:
'#(^|[^\/]|[^>])('.addcslashes($link,'.?+').')([^\w\/]|[^<]$)#i'
As such:
preg_replace('#(^|[^\/]|[^>])('.addcslashes($link,'.?+').')([^\w\/]|[^<]$)#i', "***",$strText);
The resulted string is :
<br***p>
Which is wrong!!
It should have been
<br>***<br></p>
How can I get the desired result? I have blasted my head out trying to solve this one out.
I would like to mention that str_replace replaces even the link within another valid link, so it's not a good method, I need an exact match between two boundaries, even if the boundary is text or another HTML tag.
Assuming you don't want to use a DOM parser for some reason, I believe doing what you intended is as simple as the following:
preg_replace('#(^|[^\/]|[^>])('.addcslashes($link,'.?+').')([^\w\/]|[^<]$)#i', "$1***$3",$strText);
This uses $1 and $3 to put back the delimiting text you matched in your regular expression.
As others have pointed out, using a DOM parser is more reliable.
Does this do what you want?