启用时,阵列字符串不会替换

时间:2018-09-23 17:24:00

标签: php regex replace

我有一个像这样的php字符串:

$html = '<figure class="image article-image"><img alt="In Lebron-less era of the NBA, Pudenz makes the Miami Heat look like the Jordan-Era Chicago Bulls in this stunning and proficient blowout." src="https://image.jpg" caption="true"><br /> <figcaption>In Lebron-less era of the NBA, Pudenz makes the Miami Heat look like the Jordan-Era Chicago Bulls in this stunning and proficient blowout. To say "HES ON FIRE" would be an understatement.</figcaption><br /> </figure><br /> <p>This record is a tight one to say the least. Who doesnâ&#128;&#153;t know about the over-the-top basketball antics of <em>NBA Jam</em>? Winning a game isnâ&#128;&#153;t necessarily tough, but how much you can beat a computer controlled opponent by is a whole other story. <em>Jam</em> has some AI rubber banding mechanics that come into play to try to keep things fairn which a few opponent shots made it to their basket. All these things considered make this look like itâ&#128;&#153;s going to be a record worth watching as Pudenz claims king of the hill, yet remains hungry for more. Â </p><br /> <p><iframe width="100%" height="220" allowfullscreen="allowfullscreen" frameborder="0" src="https://view.vzaar.com/13686906/player" webkitallowfullscreen="webkitallowfullscreen" mozallowfullscreen="mozallowfullscreen" allowtransparency="true" type="text/html" class="video-player" title="video player" name="vzvd-13686906" id="vzvd-13686906"></iframe></p><br /> <h2><strong><u>Arron Pitchford - <em>Game & Watch Gallery - Manhole</em> (Game Boy/Game Boy Color): Classic - Points</u></strong></h2><br /> <figure class="image article-image"><img alt="Pitchford takes the pains to make sure hundreds of little guys get safely to where they want to go, managing their frenzied and hectic moves across the bridges with incredible balance and efficiency." src="https://image1.jpg" caption="true"><br /> <figcaption>Pitchford takes the pains to make sure hundreds of little guys get safely to where they want to go, managing their frenzied and hectic moves across the bridges with incredible balance and efficiency.</figcaption><br /> </figure><br /> <p>Next we take a look at a handheld blast from the past. The <em>Game & Watch Gallery</em> was a fantastic collection of the grandfathers of all handheld gaming and <em>Manhole</em> is a great little look into the frantic nature of some of these games. p the task of bridging gaps in two structures for a d amassed an amazing 9,999 points, absolutely decimating the ssic-points/" target="_blank">previous record set on the leaderboard</a> back in 2017 (385 by s on how he could have gotten more points in his run, but with such a vast lead, he is content to sit at the top with what heâ&#128;&#153;s got for the time being. Fair enough, as it will take patience and dedication to dethrone him.</p><br /> <p><iframe width="100%" height="220" allowfullscreen="allowfullscreen" frameborder="0" src="https://view.vzaar.com/13611958/player" webkitallowfullscreen="webkitallowfullscreen" mozallowfullscreen="mozallowfullscreen" allowtransparency="true" type="text/html" class="video-player" title="video player" name="vzvd-13611958" id="vzvd-13611958"></iframe></p><br /> <h2><strong><u>Angela Stefanski - <em>Burnout Revenge</em> (Xbox 360 - NTSC): Crash - Drain Damage (Dollars)</u></strong></h2><br /> <figure class="image article-image"><img alt="You think you know a good crash in Burnout? Stefanski puts on an explosive spectacle that makes this record a vehicular maelstrom of twisted metal." src="image2.jpg" caption="true"><br /> <figcaption>You think you know a good crash in Burnout? Stefanski puts on an explosive spectacle that makes this record a vehicular maelstrom of twisted metal.</figcaption><br /> </figure><br /> <p><em>Burnout</em> is a series most certainly known for its arcade and over-the-top racing, but anyone whoâ&#128;&#153;s gotten comfy behind the wheel of these wild games know the destruction and chaos one can sow in them. In this <em>Burnout Revenge</em> record, destruction to extremes. The atch Stefanskiâ&#128;&#153;s glorious appetite for destruction in this one.</p><br /> <p><iframe width="100%" height="220" allowfullscreen="allowfullscreen" frameborder="0" src="https://view.vzaar.com/14078681/player" webkitallowfullscreen="webkitallowfullscreen" mozallowfullscreen="mozallowfullscreen" allowtransparency="true" type="text/html" class="video-player" title="video player" name="vzvd-14078681" id="vzvd-14078681"></iframe></p><br /> <h2><strong><u>Adam Hulbert - <em>Wizards of Wor</em> (Commodore 64 - Emulator): Points</u></strong></h2><br /> <figure class="image article-image"><img alt="Wizords of Wor has a ton of elements to manage if you want to lay claim to the title of Worlord Supreme. Hulbert handles the mess of monsters well to do just that." src="https://image3.jpg caption="true"><br /> <figcaption>Wizords of Wor has a ton of elements to manage if you want to lay claim to the title of Worlord Supreme. Hulbert handles the mess of monsters well to do just that.</figcaption><br /> </figure><br /> <p>This one takes us back into the Commodore 64 days with a little emulated play. tic mazes of <em>Wizards of Wor</em> to blast some beasties r 20 minutes. Itâ&#128;&#153;s enough to outdo the last <em>up a record of 148,900 back in 2016. Thereâ&#128;&#153;s a lot to consider for this run. The way the monsters move, your reflexes for killing them, and the monster that sets up double points for whole stages means players are going to need a little bit of luck and a lot of skill on their side if they want to challenge Hulbertâ&#128;&#153;s high score.</p><br /> <p><iframe width="100%" height="220" allowfullscreen="allowfullscreen" frameborder="0" src="https://view.vzaar.com/13977536/player" webkitallowfullscreen="webkitallowfullscreen" mozallowfullscreen="mozallowfullscreen" allowtransparency="true" type="text/html" class="video-player" title="video player" name="vzvd-13977536" id="vzvd-13977536"></iframe></p><br /> <h2><strong><u>Donald Hayes - <em>Centipede</em> (Arcade): Points (Tournament Settings)</u></strong></h2><br /> <figure class="image article-image"><img alt="A video almost doesnt do it justice. Donald Hayes cold concentration and reflexes are a sight to be seen." src="https://image4.jpg" caption="true"><br /> <figcaption>A video almost doesnt do it justice. Donald Hayes cold concentration and reflexes are a sight to be seen.</figcaption><br /> </figure><br /> <p>This is a record we had the pleasure of seeing in person for itâ&#128;&#153;s massive entirety. Back at Score Wars at the end of March 2018 in Santa Fe, New Mexico,  act=8&ved=2ahUKEwiY2Jr0w9jaAhUl0YMKHTrJB_wQFjAAegQ IABAo&url=/score-wars-pro-galaga-tourney-results-andrew-barrow-becomes-world-champ&usg=AOvVaw0qW2LgK-QRX_vCzVxl7Bey" target="_blank">fantastic <em>Galaga</em> play</a> wasnâ&#128;&#153;t the only thing going on. Retro gaming pros were also making attempts at their own previously set records and achieving new heights. One of those pros was <em>Centipede</em> master dible marathon lasting nearly 10 hours, Hayes pursued his previous <em>et="_blank">Centipede high score</a></em> to set a new one and did when he finally blew his last life at an incredible 7,750,817 points. His previous record sat at 7,111,111 on the <em>gs Leaderboard</a></em>. We had a chance to talk to Hayes after the fact and ask him what it took to make a score like that. He kindly shared that much of the skill doesnâ&#128;&#153;t just land in moving fast, but also knowing where to stop.</p><br /> <p>â&#128;&#156;The control over the stop-and-go of the ship with the trackball is something I feel like I can tell you about all day,â&#128;&#157; Hayes claimed. â&#128;&#156;But itâ&#128;&#153;s also the sort of muscle memory and reflexes you canâ&#128;&#153;t teach with words. Itâ&#128;&#153;s what lets me get records like these and be able to say Iâ&#128;&#153;m the best at this.â&#128;&#157; Bold, but fair words from the champ, and a record that will take dedication and practice of an incredible level to match. To see Hayes in action was a pleasure, and this is a record more than earned.</p><br /> </p>';

当我直接使用它时,我得到了结果(替换字符串):

$changeimgoriarr = array('<img alt="In Lebron-less era of the NBA, Pudenz makes the Miami Heat look like the Jordan-Era Chicago Bulls in this stunning and proficient blowout." src="https://image.jpg" caption="true">','<img alt="Pitchford takes the pains to make sure hundreds of little guys get safely to where they want to go, managing their frenzied and hectic moves across the bridges with incredible balance and efficiency." src="https://image1.jpg" caption="true">','<img alt="You think you know a good crash in Burnout? Stefanski puts on an explosive spectacle that makes this record a vehicular maelstrom of twisted metal." src="image2.jpg" caption="true">','<img alt="Wizords of Wor has a ton of elements to manage if you want to lay claim to the title of Worlord Supreme. Hulbert handles the mess of monsters well to do just that." src="https://image3.jpg caption="true">','<img alt="A video almost doesnt do it justice. Donald Hayes cold concentration and reflexes are a sight to be seen." src="https://image4.jpg" caption="true">');

$changeimgnewarr = array('<a href="https://image.jpg"><img alt="In Lebron-less era of the NBA, Pudenz makes the Miami Heat look like the Jordan-Era Chicago Bulls in this stunning and proficient blowout." src="https://image.jpg" caption="true"></a>','<a href="https://image1.jpg"><img alt="Pitchford takes the pains to make sure hundreds of little guys get safely to where they want to go, managing their frenzied and hectic moves across the bridges with incredible balance and efficiency." src="https://image1.jpg" caption="true"></a>','<a href="image2.jpg"><img alt="You think you know a good crash in Burnout? Stefanski puts on an explosive spectacle that makes this record a vehicular maelstrom of twisted metal." src="image2.jpg" caption="true"></a>','<a href="https://image3.jpg><img alt="Wizords of Wor has a ton of elements to manage if you want to lay claim to the title of Worlord Supreme. Hulbert handles the mess of monsters well to do just that." src="https://image3.jpg caption="true"></a>','<a href="https://image4.jpg"><img alt="A video almost doesnt do it justice. Donald Hayes cold concentration and reflexes are a sight to be seen." src="https://image4.jpg" caption="true"></a>');

echo str_replace($changeimgoriarr, $changeimgnewarr, $html);

用于从字符串获取图像src值的代码:

preg_match_all('/<img[^>]* src="https:\/\/[^>]*">/i', $html, $resultimg);

        $changesimgs = $resultimg[0];

        foreach ($changesimgs as $ckey => $cval) {
            $changeimgori[] = "'" .$cval ."'";
            preg_match('/"https:\/\/[^"]*"/i', $cval, $resultsrc);
            $changeimgnew[] = "'". '<a href=' . $resultsrc[0] . '>' . $cval . '</a>' ."'";
        }

        $changeimgnewarr = implode(',', $changeimgnew);
        $changeimgoriarr = implode(',', $changeimgori);

我有两个数组$ changeimgoriarr和$ changeimgnewarr

$ changeimgoriarr =是我从当前$ html字符串中获得的原始数组值;

$ changeimgnewarr =要更改的字符串,并根据上述代码创建。

我已经检查了从爆爆函数获得的两个数组值:

$changeimgoriarr = '<img alt="In Lebron-less era of the NBA, Pudenz makes the Miami Heat look like the Jordan-Era Chicago Bulls in this stunning and proficient blowout." src="https://image.jpg" caption="true">','<img alt="Pitchford takes the pains to make sure hundreds of little guys get safely to where they want to go, managing their frenzied and hectic moves across the bridges with incredible balance and efficiency." src="https://image1.jpg" caption="true">','<img alt="You think you know a good crash in Burnout? Stefanski puts on an explosive spectacle that makes this record a vehicular maelstrom of twisted metal." src="image2.jpg" caption="true">','<img alt="Wizords of Wor has a ton of elements to manage if you want to lay claim to the title of Worlord Supreme. Hulbert handles the mess of monsters well to do just that." src="https://image3.jpg caption="true">','<img alt="A video almost doesnt do it justice. Donald Hayes cold concentration and reflexes are a sight to be seen." src="https://image4.jpg" caption="true">';

$changeimgnewarr = '<a href="https://image.jpg"><img alt="In Lebron-less era of the NBA, Pudenz makes the Miami Heat look like the Jordan-Era Chicago Bulls in this stunning and proficient blowout." src="https://image.jpg" caption="true"></a>','<a href="https://image1.jpg"><img alt="Pitchford takes the pains to make sure hundreds of little guys get safely to where they want to go, managing their frenzied and hectic moves across the bridges with incredible balance and efficiency." src="https://image1.jpg" caption="true"></a>','<a href="image2.jpg"><img alt="You think you know a good crash in Burnout? Stefanski puts on an explosive spectacle that makes this record a vehicular maelstrom of twisted metal." src="image2.jpg" caption="true"></a>','<a href="https://image3.jpg><img alt="Wizords of Wor has a ton of elements to manage if you want to lay claim to the title of Worlord Supreme. Hulbert handles the mess of monsters well to do just that." src="https://image3.jpg caption="true"></a>','<a href="https://image4.jpg"><img alt="A video almost doesnt do it justice. Donald Hayes cold concentration and reflexes are a sight to be seen." src="https://image4.jpg" caption="true"></a>';

但是在基于爆破数组的字符串替换之后无法使用:

echo str_replace(array($changeimgoriarr), ($changeimgnewarr), $html);

请告诉我问题是什么,为什么在内插数组后它不起作用。

3 个答案:

答案 0 :(得分:0)

如果我的代码正确,那么您就定义了一个不应有的数组。我修复了您发布的最后一行代码:

echo str_replace($changeimgoriarr, $changeimgnewarr, $html);

str_replace替换字符串中的字符串,而不是数组中的字符串...

如果要将内含的字符串恢复为数组,则应使用explode:

$orgarr = array();
$orgarr = explode(',', $changeimgoriarr);

$newarr = array();
$newarr = explode(',', $changeimgnewarr);

答案 1 :(得分:0)

我认为您不需要单引号,也不需要内爆部分:

preg_match_all('/<img[^>]* src="https:\/\/[^>]*">/i', $html, $resultimg);

$changesimgs = $resultimg[0];

$changeimgori = array();
$changeimgnew = array();

foreach ($changesimgs as $ckey => $cval) {
    $changeimgori[] = $cval;
    preg_match('/"https:\/\/[^"]*"/i', $cval, $resultsrc);
    $changeimgnew[] =  '<a href=' . $resultsrc[0] . '>' . $cval . '</a>';
}
echo str_replace($changeimgori, $changeimgnew, $html);

答案 2 :(得分:0)

这是一个使用PHP的DOM类(http://php.net/manual/en/book.dom.php)如何实现我认为正在寻找的示例。这应该比字符串替换和preg匹配更具弹性,因为它是基于节点的:

$dom = new DOMDocument;
@$dom->loadHTML($html);

$images = $dom->getElementsByTagName('img');
foreach($images as $image) {
    $a = $dom->createElement('a'); //create new <a>
    $a->setAttribute('href', $image->getAttribute('src')); //set a's href to image's src

    $image->parentNode->insertBefore($a, $image); //insert <a>
    $image->parentNode->removeChild($image); //remove <img>

    $a->appendChild($image); //set <img> as child of <a>
}
$output = $dom->saveHTML();

echo $output;