physaux Posted November 6, 2009 Share Posted November 6, 2009 I'm having problems with my preg_match, here let me spell out exactly what is going on. I am preg matching/ searching for a URL(ex www.google.com) from the SOURCE CODE of a website. I have my function that does this, but the Prefix and Suffix is different for each SOURCE CODE of a website, 12 in total. So I have 12 unique prefixes, and 12 unique suffixes. I also have a string expression for the URL characters, which is simply: ([^ ]*) So, my preg_match code would be like so: <?php //HTTPSTRING is constant, with value of ([^ ]*) $prefix = allprefixes["site"]; $suffix = allsuffixes["site]; $regex = $prefix.HTTPSTRING.$suffix; preg_match($regex, $data, $match);//$data is just the source code of a site $finalurl = $match[1]; ?> And everything is working fine. Here are some prefixes and suffixes that I already have and have been using flawlessly: <?php $allprefixes["site1"] = '~<p class="g"><font size="-2"><b></b></font> <a href="'; $allprefixes["site2"] = '~<span class=title><a href="'; $allprefixes["site3"] = '~<p class="g"><font size="-2"><b></b></font> <a href="'; $allprefixes["site4"] = 'NONE'; $allprefixes["site5"] = '<p class=g><!--m--><a href='; $allprefixes["site6"] = '<h2 class=r><a class=l href="'; $allsuffixes["site1"] = '">~i'; $allsuffixes["site2"] = '">~i'; $allsuffixes["site3"] = '~i';//has no suffix, that is ok $allsuffixes["site4"] = '~i';//has no suffix, that is ok $allsuffixes["site5"] = '>~i'; $allsuffixes["site6"] = '">~i'; ?> And so, to the part that I am having problems with. I was adding my 7th side's prefixes to my array, when I was startled that it was (and has to be) this: resultTitle' id='infopei'><a href=" When I try to declare it as my variable, I am having trouble entering it as a string. This is because it has both ' and " . As you have noticed, my delimiter is ~ How can I store this string as my prefix, or atleast do something to make it work in the regex. I could add a "/", but I think that would mess up the regex because it is not a delimiter. Any ideas? Thanks for taking the time to read this and help me out! -I'm tired and I'm off to bed, so I'll be up to read your replies towworow! Quote Link to comment https://forums.phpfreaks.com/topic/180521-declaring-a-prefix-as-a-string-variable-with-and-and-~-as-delimiter-help/ Share on other sites More sharing options...
cags Posted November 6, 2009 Share Posted November 6, 2009 Did we not already answer this in your other thread? You just escape whichever one you use as the string delimiter (do not confuse this with the regex delimiter)... "resultTitle' id='infopei'><a href=\"" ...or... 'resultTitle\' id=\'infopei\'><a href="' Quote Link to comment https://forums.phpfreaks.com/topic/180521-declaring-a-prefix-as-a-string-variable-with-and-and-~-as-delimiter-help/#findComment-952440 Share on other sites More sharing options...
thebadbad Posted November 6, 2009 Share Posted November 6, 2009 Where did your delimiters go in the site4, site5 and site6 prefixes? Your code will fail to work when you mess up the delimiters. To include both single and double quotes in a string, you either have to escape the one used as string delimiter (not to be confused with regex delimiter) or e.g. use the heredoc syntax: $str = 'resultTitle\' id=\'infopei\'><a href="'; $str = "resultTitle' id='infopei'><a href=\""; or $str = <<<HTML resultTitle' id='infopei'><a href=" HTML; And I would incorporate the use of preg_quote() instead of your approach, to separate literal text from the regex pattern: <?php define('REGEX', '([^\s]*?)'); //quite important to make the quantifier lazy in your case, to end the match at the first occurrence of the suffixes define('DELIMITER', '~'); define('MODIFIERS', 'i'); $parts = array( array('<p class="g"><font size="-2"><b></b></font> <a href="', '">'), array('<span class=title><a href="', '">'), array('<p class="g"><font size="-2"><b></b></font> <a href="', ''), array('NONE', ''), array('<p class=g><!--m--><a href=', '>'), array('<h2 class=r><a class=l href="', '">') ); //test with first prefix and suffix $pattern = DELIMITER . preg_quote($parts[0][0], DELIMITER) . REGEX . preg_quote($parts[0][1], DELIMITER) . DELIMITER . MODIFIERS; preg_match($pattern, $data, $match); echo $match[1]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/180521-declaring-a-prefix-as-a-string-variable-with-and-and-~-as-delimiter-help/#findComment-952446 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.