Jump to content

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.