Jump to content

escaping single quote within single quotes query


devnine

Recommended Posts

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.