문제

Here's the code:

$search = $data->find("div.box_rezult");

if($data->innertext!="" and $search ){
$index = 1;
    foreach($search as $box_rezult) {

        echo  "<div id=\"header_".$index."\" class=\"headings\">".$box_rezult->find("td.l_name", 0)->plaintext;
        echo "<br/>";

Now I need to replace some words that the code parses from the html-page. For example, if there are words "flowers" and "roses" I want them to be changed to "yellow" and "red".

I'm using the code below but it doesn't work. Correct me please. Thanks!

$search = $data->find("div.box_rezult");

if($data->innertext!="" and $search ){
$index = 1;
    foreach($search as $box_rezult) {

$phrase  = $search->plaintext;
$flowers = array("flowers", "roses", "snowdrop");
$color   = array("yellow", "red", "white");

$newphrase = str_replace($flowers, $color, $phrase);

        echo  "<div id=\"header_".$index."\" class=\"headings\">".$box_rezult->find("td.l_name", 0)->plaintext.$newphrase;
        echo "<br/>";
도움이 되었습니까?

해결책

I would recommend you to use key value pairing concept. However the assumption lies that key would be unique. Its known as Hash Map data structure. In the article cited below its explained how you can do key-value implementation in php using arrays.

Is there Java HashMap equivalent in PHP?

다른 팁

This is pretty much the kind of code that gives PHP a bad name. Let's just focus on the part you care about:

$flowers = array("flowers", "roses", "snowdrop");
$color   = array("yellow", "red", "white");

$newphrase = str_replace($healthy, $yummy, $phrase);

$healthy and $yummy are undefined. Done. Watch your logs.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top