Jump to content

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

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.