Hi I am trying to use preg_match_all() to extract the number in bold out of an image URL...
http://profile.ak.fbcdn.net/hprofile-ak-snc4/174844_39677118233_8277870_t.jpg
Could someone please help me with the regular expression needed as I am stumped.
I've used this so far:
preg_match_all("(http://profile.ak.fbcdn.net/hprofile-ak-snc4/.*_t.jpg)siU", $this->html, $matching_data);
return $matching_data[0];
}
Which is just giving me an array of the full links.
Hope someone can help, thanks!!!
This will give you all occurrences:
$matches = preg_match_all ('!/hprofile-ak-snc4/[0-9]+_([0-9]+)[^/]+?\.jpg!i', $txt);
print_r ($matches);
Number you have bolded should be contained in $matches[$n][3]...
preg_match_all("#http://profile\.ak\.fbcdn\.net/(.*?)/([0-9]+)_([0-9]+)_([0-9]+)_t\.jpg#is", $string, $matches);
print_r($matches);
Try this:
([a-z][a-z0-9+\-.]*:(//[^/?#]+)?)?
([a-z0-9\-._~%!$&'()*+,;=:#/]*)
(?:(?:\d+_)(\d+)(?:_\d+))\3
I've separated it out onto multiple lines for easier reading. You will want to use capture group 4
Or (just minimized it a bit)
(?:[a-z][a-z0-9+\-.]*:(?://[^/?#]+)?)?
([a-z0-9\-._~%!$&'()*+,;=:#/]*)
(?:(?:\d+_)(\d+)(?:_\d+))\1
and use capture group 2
Related
I'm new to regex and I am really bad at it.
I've been trying to solve this problem but still can't get the result. So, I'm hoping that someone is able to assist me. thanks!
$str = "/tqrfq_58533_13";
preg_match_all('/\d+(?>=_)*/', $str, $matches);
print_r($matches); // gets 58533, 13
but I only want '58533' and not both numbers. So I want the array of $matches to return '58533' as the only number
Use /(?<=_)(\d+)(?=_)/ as pattern in preg_match() that match digits between _
$str = "/tqrfq_58533_13";
preg_match('/(?<=_)(\d+)(?=_)/', $str, $matches);
echo $matches[0];
// 58533
Check result in demo
Also you can use preg_replace() if you don't want to get array as result
echo preg_replace('/.*?_(\d+)_.*/', "$1", $str);
// 58533
preg_match_all('/\d+(?=_)/', $str, $matches);
If you want to get only one number, remove * part since it means the result will be more than one. AFAIK, there is no such things like (?>=_). I use (?=_) to indicate that _ immediately follow the number.
You can see this link for more clarification.
I'm a regex-noobie, so sorry for this "simple" question:
I've got an URL like following:
http://stellenanzeige.monster.de/COST-ENGINEER-AUTOMOTIVE-m-w-Job-Mainz-Rheinland-Pfalz-Deutschland-146370543.aspx
what I'm going to archieve is getting the number-sequence (aka Job-ID) right before the ".aspx" with preg_replace.
I've already figured out that the regex for finding it could be
(?!.*-).*(?=\.)
Now preg_replace needs the opposite of that regular expression. How can I archieve that? Also worth mentioning:
The URL can have multiple numbers in it. I only need the sequence right before ".aspx". Also, there could be some php attributes behind the ".aspx" like "&mobile=true"
Thank you for your answers!
You can use:
$re = '/[^-.]+(?=\.aspx)/i';
preg_match($re, $input, $matches);
//=> 146370543
This will match text not a hyphen and not a dot and that is followed by .aspx using a lookahead (?=\.aspx).
RegEx Demo
You can just use preg_match (you don't need preg_replace, as you don't want to change the original string) and capture the number before the .aspx, which is always at the end, so the simplest way, I could think of is:
<?php
$string = "http://stellenanzeige.monster.de/COST-ENGINEER-AUTOMOTIVE-m-w-Job-Mainz-Rheinland-Pfalz-Deutschland-146370543.aspx";
$regex = '/([0-9]+)\.aspx$/';
preg_match($regex, $string, $results);
print $results[1];
?>
A short explanation:
$result contains an array of results; as the whole string, that is searched for is the complete regex, the first element contains this match, so it would be 146370543.aspx in this example. The second element contains the group captured by using the parentheeses around [0-9]+.
You can get the opposite by using this regex:
(\D*)\d+(.*)
Working demo
MATCH 1
1. [0-100] `http://stellenanzeige.monster.de/COST-ENGINEER-AUTOMOTIVE-m-w-Job-Mainz-Rheinland-Pfalz-Deutschland-`
2. [109-114] `.aspx`
Even if you just want the number for that url you can use this regex:
(\d+)
I am trying to get the 6 or 7 number sequence and put it in the urls array.
<a href="/product/view/4539922/" class="raw_clafd">
However I am having a problem with the regex below.
preg_match_all('/<a\s+href="\.\/view\/(\d{6,7})\/" class="raw_clafd">/', $str, $urls);
What am I missing? Thank you
You cannot match /product with \.
You can use:
preg_match_all('#<a\s+href="/product/view/(\d{6,7})/"\s+class="raw_clafd">#', $str, $urls);
But I really believe you should consider using DOM parser.
You can get the value after /view/ just by using
/\/view\/(\d{6,7})/
there are a lot of topics like this one but i don't know what the error i tried a lot
so this is the original text
onclick="NewWindow('http://google.com','name','800','600','yes');return false">
this is my code
$re1='(onclick)';
$re2='(=)';
$re3='(.)';
$re4='(NewWindow)';
$re5='(\\()';
$re6='(.)';
$re7='((?:http|https)(?::\\/{2}[\\w]+)(?:[\\/|\\.]?)(?:[^\\s"]*))';
$c=preg_match_all ("/".$re1.$re2.$re3.$re4.$re5.$re6.$re7."/is", $txt, $matches);
print_r($matches);
any one can help me to get the url using regular expression and php??
what is the wrong with this code?
Regards
preg_match("/NewWindow\('([^']*)'/",$txt, $matches);
matches[1] contains the url
is it what you need ?
(edit: put in code block because a parenthesis was not escaped correclty
This should work:
preg_match("/onclick=\"NewWindow\('(.*)','n/",$txt,$matches);
I'd use non-greedy matching for this:
preg_match("/onclick=\"NewWindow\('(.*?)'/", $txt, $matches);
Based on your description, the regex I would use, would be:
/(?<=NewWindow\(\').*(http://|https://)[^\'\"]*/i
or
/(?<=onclick=\"NewWindow\(\').*(http://|https://)[^\'\"]*/i
A great tool for testing your regex is: http://gskinner.com/RegExr/
It outputs just the url and only does so if it is preceded by "NewWindow('" in the first example or "onclick="NewWindow('", which means, in your case, 'http://google.com').
I need to get string from comment in HTML file, I was trying to do it with DOM, but I didn't find good solution with this method.
So I want to try it with regular expressions, but I can't find satisfactory solution. Please, can you help me?
This is what I need:
<!--adress-"String here I need to get"-->
Thanks in advance for answer
Look into $matches after this code
preg_match('~<!--adress-"(.*?)"-->~msi', $string, $matches);
HTML comments are regular; you can just match <!--adress-"([^">]+)"--> and get the first group.
This assumes that the comments are always well-formed and always have a quoted string containing no quotes.
It will be more accurate:
$regex = '<!--(.+?)-"{0,1}(.+?)"{0,1}-->';
preg_match_all($regex, $html, $matches_array);
Just do the var_dump($matches_array) and see results.