physaux Posted November 4, 2009 Share Posted November 4, 2009 Prefix: <p class="g"><font size="-2"><b></b></font> <a href=" Pattern: ^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix Suffix: "> How can I make that into a preg_match() expression, I can't get it to work! I have tried: preg_match($prefix."(".$pattern.")".$suffix, $result); Should that work? Or any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/180349-my-preg_match-isnt-working/ Share on other sites More sharing options...
salathe Posted November 5, 2009 Share Posted November 5, 2009 Use preg_quote on the prefix and suffix strings to help prevent any conflicting characters that might be in there. Also, your pattern (the entire first argument to preg_match) needs delimiters at the start and end, the default is a forward-slash (/) character. The string containing the "pattern" probably does not need ^ nor $ and almost certainly doesn't need the /ix in it (that last bit should probably be right at the very end of everything. Assuming your prefix, expression and suffic are correct then it's likely you'd rather want: Pattern (just removed bits from the start/end): (http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)? Code: $regex = '/' . preg_quote($prefix) . '(' . $pattern . ')' . preg_quote($suffix) . '/ix'; echo $regex; // Just to see what it looks like preg_match($regex, $subject, $match); Hopefully that'll help a bit, if not just ask. Quote Link to comment https://forums.phpfreaks.com/topic/180349-my-preg_match-isnt-working/#findComment-951380 Share on other sites More sharing options...
physaux Posted November 5, 2009 Author Share Posted November 5, 2009 Yyeaa, it's not working. I tried echo'ing and printing the prefix and suffix both preg_quoted and not, as well as the regex. They all give me random combinations of "/\\\/\/\/\" What is going on? Maybe the browser is interpreting the code as html code, is there any way for me to print it so that it only shows the string, not "renders" it? Quote Link to comment https://forums.phpfreaks.com/topic/180349-my-preg_match-isnt-working/#findComment-951411 Share on other sites More sharing options...
physaux Posted November 5, 2009 Author Share Posted November 5, 2009 I can't edit anymore, but I wanted to add this: Could it be relevant that what the "content" is is (I think) the source code (or is it rendered ouput?) of a URL. Here is the function that generates it: <?php $ch = curl_init(); $timeout = 5; curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); $data = curl_exec($ch); curl_close($ch); ?> So my prefix and suffix is based on what was surrounding my target when I "viewed source" manually at the target URL Quote Link to comment https://forums.phpfreaks.com/topic/180349-my-preg_match-isnt-working/#findComment-951425 Share on other sites More sharing options...
thebadbad Posted November 5, 2009 Share Posted November 5, 2009 That would be the source code you're retrieving. If you 'trust' the URL you're grabbing, you could simply do <?php //$data holds the source code of the remote page preg_match('~<p class="g"><font size="-2"><b></b></font> <a href="([^"]*)">~i', $data, $match); echo $match[1]; ?> Assuming the prefix and suffix actually match with the source code. Else if you want to keep your URL pattern, try this, using a modified version of the pattern you provided (it had some errors/opportunities for improvement): <?php preg_match('~<p class="g"><font size="-2"><b></b></font> <a href="(https?://[a-z0-9]+(?:[-.][a-z0-9]+)*\.[a-z]{2,6}(?::[0-9]{1,5})?(?:/.*?)?)">~is', $data, $match); echo $match[1]; ?> @salathe You forgot to add the delimiter as the second parameter to preg_quote(). Quote Link to comment https://forums.phpfreaks.com/topic/180349-my-preg_match-isnt-working/#findComment-951617 Share on other sites More sharing options...
salathe Posted November 5, 2009 Share Posted November 5, 2009 @salathe You forgot to add the delimiter as the second parameter to preg_quote(). Oops. It was late and was only running on half-steam, my apologies. Quote Link to comment https://forums.phpfreaks.com/topic/180349-my-preg_match-isnt-working/#findComment-951652 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.