I need to extract p5925 and c98 of the following URL:
http://www.example.com/mens-shoes/mens-boots/p5925/c98/colour/Black
The format I want is:
p:5925, c:98
The regex I got now is ([^pc]\d+) which matches just the numbers but not sure how I can get it in the format I wanted.
I used the code preg_match('/([^pc]\d+)/', $ls_url, $la_matches); to split it.
You need to use preg_match_all
preg_match_all('~(?<=/)([pc])(\d+)(?=/)~', $ls_url, $la_matches);
or
preg_match_all('~\b([pc])(\d+)\b~', $ls_url, $la_matches);
DEMO
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 need some help creating regular expressions to pick information out of a file. I am using php preg_match to do it and am trying to get information that looks like the following:
ex.
19-Aug-2013 //The date will always be in the format.
and a number like this
303.00
The file I am trying to get this information from is the body of a mime type email.
I only need these two types of specific information.
Try this as a whole:
preg_match_all('/(\d{2}-\w{3}-\d{4}|\d+\.\d{1,2})/', $file, $matches);
\d{2}-\w{3}-\d{4}: Get the date
\d+\.\d{1,2}: Get the float
By using preg_match_all you return all found matches in all the haystack. preg_match only matches the first occurance.
For the date you can use:
[0-9]{2}-[a-z]{3}-[0-9]{4}
For the number:
[0-9]+\.[0-9]{2}
With no specific input is hard to come up with a better regex at the moment...
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);
Im trying to extract a YouTube link from just random text. e.g.
This is some random text and url is http://www.youtube.com/watch?v=-d3RYW0YoEk&feature=channel and I want to pull this URL out of this text in PHP. Can't seem to figure it out. Found a solution in another language but don't know how to convert it.
Thanks for the help.
You can use preg_match_all to grab all such URL's as:
if(preg_match_all('~(http://www\.youtube\.com/watch\?v=[%&=#\w-]*)~',$input,$m)){
// matches found in $m
}
you could try to use Regex
http://php.net/manual/en/function.preg-match.php
Use preg_match.
The pattern should be something like:
/(http\:\/\/www\.youtube\.com\/watch\?v=\w{11})/