hellzone Posted December 8, 2007 Share Posted December 8, 2007 Hi all i'm trying to match all items in a string that start with and end with I'm useless with Regex expressions and after i've completed this project i will be spending a lot of my time learning them but for now I need this help urgently. that is my current expression: preg_match_all ("\[url\].+\[\/url\]",$text,$matches); with the string "http://http://Hello i'm some urltexthttp://www.google.co.ukhttp://" It matches the whole string because the start and finish are and . It does'nt matter what the data in between is but it does matter that the expression find only one instance and then move to the next. Please can someone help, i'm sure this is very simple Thanks James Quote Link to comment Share on other sites More sharing options...
dsaba Posted December 8, 2007 Share Posted December 8, 2007 Hi all i'm trying to match all items in a string that start with and end with I'm useless so you were explaining something and then you stopped? I'm useless with Regex expressions and after i've completed this project i will be spending a lot of my time learning them but for now I need this help urgently. bla bla bla, what's this have to do with your question? So I didn't quite understand your problem, you're trying to find urls that have http:// in them twice?? Quote Link to comment Share on other sites More sharing options...
hellzone Posted December 9, 2007 Author Share Posted December 9, 2007 I do appologise thats because the forum changed my tags into html tags!!! I have a string [pre]"(url)http://(/url)(url)(/url)(url)www.google.com(/url)this is some url text(url)(/url)" [/pre] which i'm trying to get the individual web address's from but everytime I do I seem to get everything inside the first [pre](url)[/pre] and the last [pre] (/url) [/pre] the code i'm using is [pre] preg_match_all ("\(url\).+\(\/url\)",$text,$matches); [/pre] So wondering how to i stop it finding the entire string and just find the single instances? THanks James Quote Link to comment Share on other sites More sharing options...
jcd Posted December 9, 2007 Share Posted December 9, 2007 Try: preg_match_all('/(?<=\[url\])[^\[]+?(?=\[\/url\])/i',$text,$matches); Quote Link to comment Share on other sites More sharing options...
dsaba Posted December 9, 2007 Share Posted December 9, 2007 try <?php $haystack = '<a id="topofpage" name="topofpage"></a> <ul class="access"> <li><a href="/div/kidspace/" title="Go to the home page of this website. [ALT + 1 Windows, CONTROL + 1 Mac, then press ENTER/RETURN if using IE]" accesskey="1">Go to the home page for this website.</a></li> <li><a href="#startcontent" title="Go to the main content. [ALT + 2 Windows, CONTROL + 2 Mac, then press ENTER/RETURN if using IE]" accesskey="2">Go to the main content for this page.</a></li> <li><a href="#skiptonavigation" title="Go to the main navigation for this page. [ALT + 3 Windows, CONTROL + 3 Mac, then press ENTER/RETURN if using IE]" accesskey="3">Go to main navigation for this page.</a></li> <li><a href="#topofpage" title="Go to the top of this page. [ALT + 4 Windows, CONTROL + 4 Mac, then press ENTER/RETURN if using IE]" accesskey="4">Go to the top of this page.</a></li> <li><a href="/div/sitesearch/" title="Go to the search tool for this website. [ALT + 5 Windows, CONTROL + 5 Mac, then press ENTER/RETURN if using IE]" accesskey="5">Go to the search tool for this website.</a></li> <li><a href="/div/contact/" title="Go to the contact page for this website. [ALT + 6 Windows, CONTROL + 6 Mac, then press ENTER/RETURN if using IE]" accesskey="6">Go to the contact page for this website.</a></li> <li><a href="/div/ask/" title="Go to our Ask A Question reference service. [ALT + 7 Windows, CONTROL + 7 Mac, then press ENTER/RETURN if using IE]" accesskey="7">Go to our Ask A Question reference service.</a></li> <li><a href="/div/accessibility/" title="Go to the accessibility information page for this website. [ALT + 0 Windows, CONTROL + 0 Mac, then press ENTER/RETURN if using IE]" accesskey="0">Go to the accessibility information page for this website.</a></li> '; $leftM = 'href="'; $rightM = '"'; $pat = '~'.$leftM.'((??!'.$rightM.').)+)'.$rightM.'~'; preg_match_all($pat, $haystack, $out); print_r($out); ?> this only works for links enclosed in " double quotes btw Quote Link to comment Share on other sites More sharing options...
dsaba Posted December 9, 2007 Share Posted December 9, 2007 here's a modified version of my other post to allow single quoted href tags as well <?php $pat = '/href=[\'"]((??![\'"]).)+)[\'"]/'; ?> Quote Link to comment Share on other sites More sharing options...
dsaba Posted December 9, 2007 Share Posted December 9, 2007 So wondering how to i stop it finding the entire string and just find the single instances? to answer your other question: with preg_match_all($pat, $str, $out); where $out is the output array of matches $out[0] will have the complete match with the pattern included $out[1-....] will have substrings that you specify in the pattern to specify these substrings to match simply enclose regex statements in ( ) parenthesis so for example in your original post, to get the substring (of the actual url) to spit out in $out[1] you would say: preg_match_all ("\[url\](.+)\[\/url\]",$text,$matches); ^^ there's the substring enclosed in parenthesis /href=[\'"]((??![\'"]).)+)[\'"]/ ^^^^^^^^^ in the pattern I gave you here it is as the most outer set of parenthesis -happy coding Quote Link to comment Share on other sites More sharing options...
hellzone Posted December 9, 2007 Author Share Posted December 9, 2007 Thanks a lot guys that helped a lot it's all working now Quote Link to comment 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.