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.
Related
I think I need to use the preg_replace function but not sure exactly how to type in the patterns I want to find and replace. Basically, I want to replace this:
: u"x"x",
with this:
: u"x'x",
x means that any characters can go there. But I don't know how to write the x in PHP.
Thank you!
Edit: basically, I want to replace that middle double-quote with a single-quote. And I'll be searching through a big JSON file to do it. Probably should have said this at the start.
You could use this regular expression:
$result = preg_replace('#(: u".*?)"(.*?")#', "$1'$2", $string);
i'm searching for keywords in a string via a regular expression. It works fine for all keywords, exept one which contains a forward slash in it: "time/emit" .
Even using preg_quote($find,'/'), which escapes it, i still get the message:
Unknown modifier 't' in /frontend.functions.php on line 71
If i print the find pattern, it shows /time\\/emit/ . Without preg_quote, it shows /time/emit/ and both return the same error message.
Any bit of knowledge would be useful.
Try to begin and end your regular expression with different sign than /
I personally use `
I've seen people using #
I think most chars are good. You can read more about it here: http://pl.php.net/manual/en/regexp.reference.delimiters.php
Like this:
preg_match('#time/emit#', $subject); // instead of /time/emit/
To put it another way: Your $find variable should contain rather #time/emit# than /time/emit/
looks like you have something already escaping it..
preg_quote('time/emit') // returns time\/emit
preg_quote('time\/emit') // returns time\\/emit
as a hack you could simply do:
preg_quote(stripslashes($find)) // will return time\/emit
bit of code?
the the 'regex' for that particular term should look something like '/time/emit/'. With a set of keywords there may be a more efficient method so seeing what you are doing would be good.
this should work:
$a="Hello////////";
$b=str_replace($a,"//","/");
echo $b;
I have the following url. http://domain.com/userfiles/dynamic/images/whatever_dollar_1318105152.png
Everything in the url can change except the userfiles part and the last underscore. Basically I want to get the part of the url which is userfiles/dynamic/images/whatever_dollar_ What is a good way to do this. I'm open or both JavaScript or php.
Use parse_url in PHP to split an url in its various parts. Get the path part that is returned. It contains the path without the domain and the query string.
After that use strrpos to find the last occurrance of the _ within the path.
With substr you can copy the first part of the path (up until the found _) and you're done.
You could, with JavaScript, try:
var string = "http://domain.com/userfiles/dynamic/images/whatever_dollar_1318105152.png";
var newString = string.substring(string.indexOf('userfiles'),string.lastIndexOf('_'));
alert(newString); // returns: "userfiles/dynamic/images/whatever_dollar" (Without quotes).
JS Fiddle demo.
References:
substring().
indexOf().
lastIndexOf().
Assuming your string is stored in $s, simply:
echo preg_replace('/.*(userfiles.*_).*/', '$1', $s);
i'm searching for keywords in a string via a regular expression. It works fine for all keywords, exept one which contains a forward slash in it: "time/emit" .
Even using preg_quote($find,'/'), which escapes it, i still get the message:
Unknown modifier 't' in /frontend.functions.php on line 71
If i print the find pattern, it shows /time\\/emit/ . Without preg_quote, it shows /time/emit/ and both return the same error message.
Any bit of knowledge would be useful.
Try to begin and end your regular expression with different sign than /
I personally use `
I've seen people using #
I think most chars are good. You can read more about it here: http://pl.php.net/manual/en/regexp.reference.delimiters.php
Like this:
preg_match('#time/emit#', $subject); // instead of /time/emit/
To put it another way: Your $find variable should contain rather #time/emit# than /time/emit/
looks like you have something already escaping it..
preg_quote('time/emit') // returns time\/emit
preg_quote('time\/emit') // returns time\\/emit
as a hack you could simply do:
preg_quote(stripslashes($find)) // will return time\/emit
bit of code?
the the 'regex' for that particular term should look something like '/time/emit/'. With a set of keywords there may be a more efficient method so seeing what you are doing would be good.
this should work:
$a="Hello////////";
$b=str_replace($a,"//","/");
echo $b;
I need to preg_match for
src="http:// "
where the blank space following // is the rest of the url ending with the ". My adapted doesn't seem to work:
preg_match('#src="(http://[^"]+)#', $data, $match);
And I am also struggling to get text that starts with > and ends with EITHER a full stop . or an exclamation mark ! or a question mark ? I have no idea how to do this one. An example of the text I want to preg_match for is:
blahblahblah>Hello world this is what I want.
I'm hoping a kind preg_match guru can tell me the answer and save me hours of headscratching.
Thanks for reading.
As for the URL:
preg_match('#src="(.*?)"#', $data, $match);
and for the second case, use />(.*?)(\.|!|\?)/
(.*?)" will match any character greedily up until the time it sees the end double quote
It seems that you want to parse a document or string which follows a HTML, DOM, XML or something similiar structure.
Use XPath, and parse to the Tag and let it return the src Attribute, this will save much trouble and you can forget about regular expressions.
Example: CLICK ME