【问题标题】:On Implode an array string not replacedOn Implode 未替换的数组字符串
【发布时间】:2019-02-27 07:52:47
【问题描述】:

我有一个这样的 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 = 要更改并从上述代码创建的字符串。

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

$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);

请告诉我内爆数组后为什么它不起作用的问题。

【问题讨论】:

  • 你能解释一下你想用这段代码实现什么吗?
  • 我希望得到如下结果:锚标签内的图像标签 src 值示例: 我想要这样 image.jpg"><imgsrc="image.jpg">
  • 您是否要提取src="" 中的值,然后将其添加为href=""?那你还需要src 属性吗?或者只做str_replace('src=', 'href=', $html); 就足够了(尽管你可能想要 preg_replace 因为像src = 这样的变体)
  • 这些单引号"'" 的用途是什么?这里:$changeimgori[] = "'" .$cval ."'"; ?
  • 我还需要 src 值,并在锚标签中添加图像标签,并在 href 中添加图像标签的 src 值。我需要机器人,但这样:image.jpg"><imgsrc="image.jpg"></a>

标签: php regex replace


【解决方案1】:

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

$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;

【讨论】:

    【解决方案2】:

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

    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);
    

    【讨论】:

    • 谢谢你我得到了结果......非常感谢你的回复它对我有用。
    【解决方案3】:

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

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

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

    如果你希望你的内爆字符串返回数组,那么你应该使用explode:

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

    【讨论】:

      猜你喜欢
      • 2014-11-13
      • 1970-01-01
      • 2012-04-26
      • 1970-01-01
      • 2014-03-21
      • 2013-11-08
      • 2010-09-28
      • 1970-01-01
      • 2013-10-19
      相关资源
      最近更新 更多