willdk Posted December 4, 2008 Share Posted December 4, 2008 I have a list with html urls <a href="http://www.website.tld/home.php?search=1&results=20&query=cool+website">cool website</a>, I can match <a href="http://www.website.tld/home.php?search=1&results=20&query= by using <a href=\x22http://www.website.tld/home.php?search=1&results=20&query= But I have a problem with the part between 'query=' and '">'. This can be everything: letters, digits, quotes, /,... How do I tell php to match all possible characters and length? Quote Link to comment https://forums.phpfreaks.com/topic/135554-solved-match-everything-between-and-x22/ Share on other sites More sharing options...
nrg_alpha Posted December 4, 2008 Share Posted December 4, 2008 Do I undrestand you corrcetly when I propose this? $str = '<a href="http://www.website.tld/home.php?search=1&results=20&query=cool+website">cool website</a>'; preg_match('#<a href="([^"]*)"#', $str, $match); echo $match[1]; Output: http://www.website.tld/home.php?search=1&results=20&query=cool+website EDIT: Admittedly, your post is not very descriptive, in that you are not showing on a line by itself a sample of what exactly you are trying to match (based on your initial a tag). If I am off base on what I am proposing, then I am misunderstanding what it is exactly you are looking for. Give me a sample match portion from your initial example string (in otherwords, show me exactly what part you want to match from your string). Beacuse this thread is called match everything between = and \x22>, but yet in the example, you have: <a href=\x22http://www.website.tld/home.php?search=1&results=20&query= well, the = and \x22 is right beside each other.. so its confusing. If I did nail it, then it's all good. Quote Link to comment https://forums.phpfreaks.com/topic/135554-solved-match-everything-between-and-x22/#findComment-706283 Share on other sites More sharing options...
willdk Posted December 4, 2008 Author Share Posted December 4, 2008 My post was indeed not very clear as it's the first time I'm working with regex. I made a mistake by using str_replace and not preg_replace The code you gave me (I added a dot after ") helped me, but I still didn't get the output right. #<a href="([^"]*)".# This is wat I want to get as output; 1. I have a list with urls <a href="http://www.website.tld/home.php?search=1&results=20&query=cool+website">cool website</a>, <a href="http://www.website.tld/home.php?search=1&results=20&query=best+forum">best forum</a>, <a href="http://www.website.tld/home.php?search=1&results=20&query=tryme">tryme</a>, ... 2. I just want to show the part between '>' and '</a>, ' with a '<br>' at the end cool website<br> best forum<br> tryme<br> ... Quote Link to comment https://forums.phpfreaks.com/topic/135554-solved-match-everything-between-and-x22/#findComment-706348 Share on other sites More sharing options...
nrg_alpha Posted December 5, 2008 Share Posted December 5, 2008 Ah, well that explains things much more clearly... try this: $str = '<a href="http://www.website.tld/home.php?search=1&results=20&query=tryme">tryme</a>'; preg_match('#<a[^>]*>(.+?)</a>#', $str, $match); echo $match[1]; Output: tryme Quote Link to comment https://forums.phpfreaks.com/topic/135554-solved-match-everything-between-and-x22/#findComment-706354 Share on other sites More sharing options...
nrg_alpha Posted December 5, 2008 Share Posted December 5, 2008 As far as the <br> at the end, you can simply add this to the $match result manually Quote Link to comment https://forums.phpfreaks.com/topic/135554-solved-match-everything-between-and-x22/#findComment-706357 Share on other sites More sharing options...
willdk Posted December 5, 2008 Author Share Posted December 5, 2008 your code works with the string. thank you Now I'm trying to run this for the complete list. I made a code (on the base of some other codes), but I only get a few words ??? <?php $file = file_get_contents('urls.php'); $expl = explode("\n",$file); $new = implode("\n",$expl); preg_match('#<a[^>]*>(.+?)</a>#', $new, $match); echo $match[1]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/135554-solved-match-everything-between-and-x22/#findComment-706370 Share on other sites More sharing options...
nrg_alpha Posted December 5, 2008 Share Posted December 5, 2008 Ah, you are getting these links in a file. I suppose one way would be to use preg_match_all instead of just preg_match, and apply that to $file: $file = file_get_contents('urls.php'); preg_match_all('#<a[^>]*>(.+?)</a>#s', $file, $matches); echo '<pre>'.print_r($matches[1], true); Would this work out? Quote Link to comment https://forums.phpfreaks.com/topic/135554-solved-match-everything-between-and-x22/#findComment-706378 Share on other sites More sharing options...
willdk Posted December 5, 2008 Author Share Posted December 5, 2008 I get the keywords with numbers and 'array' at the beginning. Did I do something wrong? ??? Array ( [0] => keyword1 [1] => keyword2 [2] => keyword3 [3] => keyword4 ... Quote Link to comment https://forums.phpfreaks.com/topic/135554-solved-match-everything-between-and-x22/#findComment-706398 Share on other sites More sharing options...
nrg_alpha Posted December 5, 2008 Share Posted December 5, 2008 lol not at all.. sorry about that...its because of how I presented the end results with this last line: echo '<pre>'.print_r($matches[1], true); You don't have to use that if you don't want to.. you could just use a foreach function to tap into $matches[1] and go that way instead: foreach($matches[1] as $val){ echo $val . '<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/135554-solved-match-everything-between-and-x22/#findComment-706405 Share on other sites More sharing options...
willdk Posted December 5, 2008 Author Share Posted December 5, 2008 Now it works Thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/135554-solved-match-everything-between-and-x22/#findComment-706410 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.