Jump to content

Parsing text


OriginalDavid

Recommended Posts

I'm trying to use PHP to parse data from a website.

 

I've used the explode function to get rid of all the other code except the bit I need.

 

The bit I'm left with looks like this:

 

 

newsText[0] = "News Headline 1";

newsLink[0] = "/link-to-story1.html";

newsText[1] = "News Headline 2";

newsLink[1] = "/link-to-story2.html";

newsText[2] = "News Headline 3";

newsLink[2] = "/link-to-story3.html";

 

 

What I want to do is get each headline in an array and each corresponding link in an array.  Ideally if I could keep the same names of the arrays as above that would be good. Simply I'd like to add a $ symbol on to each line so suddently newsText[0] becomes $newsText[0] - then I get use the data in my script.

 

Thanks for your help.

 

David

 

Link to comment
https://forums.phpfreaks.com/topic/142367-parsing-text/
Share on other sites

so I think what you're saying is...

 

newsText[0] = "News Headline 1";

newsLink[0] = "/link-to-story1.html";

newsText[1] = "News Headline 2";

newsLink[1] = "/link-to-story2.html";

newsText[2] = "News Headline 3";

newsLink[2] = "/link-to-story3.html";

 

is the literal text in your string or file or whatever, right?  And you're wanting it to basically be as if you were writing physical code to assign those "....."; to those news...[..] arrays, right?

 

Link to comment
https://forums.phpfreaks.com/topic/142367-parsing-text/#findComment-745972
Share on other sites

well anyways, if ^^ is what you mean, then you can do this:

 

<?php
$string = '
newsText[0] = "News Headline 1";
newsLink[0] = "/link-to-story1.html";
newsText[1] = "News Headline 2";
newsLink[1] = "/link-to-story2.html";
newsText[2] = "News Headline 3";
newsLink[2] = "/link-to-story3.html";
';

$code = explode("\n",$string);
$code = array_filter($code);
foreach ($code as $key => $exp) {
   eval("$" . $exp);
}

echo "<pre>"; print_r($newsText); echo "</pre>";
echo "<pre>"; print_r($newsLink); echo "</pre>";
?>

Link to comment
https://forums.phpfreaks.com/topic/142367-parsing-text/#findComment-745977
Share on other sites

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.