devnine Posted June 29, 2011 Share Posted June 29, 2011 I'm trying to write a web scraper for the source code of a webpage. The regex expersssion I'm using is (.+?) which means match everything from here until the delimiter which is a ' character. $regex = '/&kingdom=(.+?) \' /'; preg_match($regex,$data,$match); var_dump($match); echo $match[1]; The problem is that I can' delimit at the ' character.I've tried escaping it like \' but that doesn't work. How do I use the ' character correctly inside single quotes? Thanks. Link to comment https://forums.phpfreaks.com/topic/240747-escaping-single-quote-within-single-quotes-query/ Share on other sites More sharing options...
Andy-H Posted June 29, 2011 Share Posted June 29, 2011 \\' :confused: Link to comment https://forums.phpfreaks.com/topic/240747-escaping-single-quote-within-single-quotes-query/#findComment-1236581 Share on other sites More sharing options...
devnine Posted June 29, 2011 Author Share Posted June 29, 2011 Thanks but I get this Warning: Unexpected character in input: ''' (ASCII=39) state=1 in Parse error: syntax error, unexpected ';' in Here is the entire code: <?php $data = file_get_contents('http://kingdomgame.net/Kingdoms/?a=viewKingdoms&world=W10'); $regex = '/&kingdom=(.+?) \\' /'; preg_match($regex,$data,$match); var_dump($match); echo $match[1]; ?> The source code look like this viewKingdom&world=W10&kingdomID=9070&kingdom=Shock'>Shock</a></td><td and what I'm trying to extract is the text between &kingdom= and ' The output should be Shock in this example. Link to comment https://forums.phpfreaks.com/topic/240747-escaping-single-quote-within-single-quotes-query/#findComment-1236583 Share on other sites More sharing options...
Andy-H Posted June 29, 2011 Share Posted June 29, 2011 <?php $data = file_get_contents('http://kingdomgame.net/Kingdoms/?a=viewKingdoms&world=W10'); $regex = "~&kingdom=(.+?)'~"; preg_match($regex,$data,$match); var_dump($match); echo $match[1]; ?> Link to comment https://forums.phpfreaks.com/topic/240747-escaping-single-quote-within-single-quotes-query/#findComment-1236590 Share on other sites More sharing options...
devnine Posted June 29, 2011 Author Share Posted June 29, 2011 Thanks. I get the following array(2) { [0]=> string(15) "&kingdom=Shock'" [1]=> string(5) "Shock" } Shock However I am looking for just the text between the = and the ' ie:Shock whereas now I am getting &kingdom=Shock as the string. Link to comment https://forums.phpfreaks.com/topic/240747-escaping-single-quote-within-single-quotes-query/#findComment-1236591 Share on other sites More sharing options...
Andy-H Posted June 29, 2011 Share Posted June 29, 2011 Form the var_dump $match[1] appears to be "Shock" ?? Link to comment https://forums.phpfreaks.com/topic/240747-escaping-single-quote-within-single-quotes-query/#findComment-1236593 Share on other sites More sharing options...
devnine Posted June 29, 2011 Author Share Posted June 29, 2011 Yes you are indeed correct. Time to learn about the var_dump function. Thanks very much. Link to comment https://forums.phpfreaks.com/topic/240747-escaping-single-quote-within-single-quotes-query/#findComment-1236596 Share on other sites More sharing options...
Andy-H Posted June 29, 2011 Share Posted June 29, 2011 No worries. var_dump I find for arrays echo '<pre>' . print_r($array, true) . '</pre>'; print_r is much more readable. Link to comment https://forums.phpfreaks.com/topic/240747-escaping-single-quote-within-single-quotes-query/#findComment-1236600 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.