找到字符串模式并替换它〜

时间:2011-06-25 08:28:47

标签: php regex

我有一堆html字符串。(原始字符串)

Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
Cras a egestas lacus. Praesent non ante in dui pellentesque 
molestie. Fusce facilisis, libero eu pellentesque varius, 
est magna pretium urna, id dictum urna tortor eget neque. 
Sed a arcu a lectus euismod volutpat. Aenean est urna, 
ultricies quis consequat sit amet, pretium a massa. 
Praesent sit amet nisl nec dolor malesuada posuere. 
Phasellus ac urna eget ligula vehicula cursus. Donec
 volutpat dui egestas dolor aliquet interdum. Vestibulum 
 ante ipsum primis in faucibus orci luctus et ultrices 
 posuere cubilia Curae; Duis sed neque sed mauris gravida 
 rhoncus. 
 <ul class="image-info">
    <li id="ID5555-1" class="image-size"></li>
    <li id="ID5555-2" class="image-height"></li>
    <li id="ID5555-3" class="image-width"></li>
</ul>
<div id="ID5555-4"  class="file-preview"></div>
<div id="ID5555-5"  class="file-detail"></div>

Cum sociis natoque penatibus et magnis dis parturient montes, 
nascetur ridiculus mus. Nam eu egestas ipsum. Duis ultrices 
vehicula metus, sit amet convallis libero suscipit ac.

<ul class="image-info">
    <li id="ID7777-1" class="image-size"></li>
    <li id="ID7777-2" class="image-height"></li>
    <li id="ID7777-3" class="image-width"></li>
</ul>
<div id="ID7777-4"  class="file-preview"></div>
<div id="ID7777-5"  class="file-detail"></div>

 Nullam libero lorem, suscipit in imperdiet ac, porttitor sit amet turpis.
 Cras tincidunt imperdiet eros, id sagittis felis pellentesque sit amet. 
 Mauris ultrices nulla ac dolor fermentum at feugiat erat egestas. Quisque 
 dapibus tellus eu dolor sagittis consequat. Maecenas dapibus dolor et nunc 
 sollicitudin laoreet. Sed lacus est, pharetra a fringilla eu, ornare varius 
 lacus. Etiam at lorem in leo molestie interdum ut vitae diam. Morbi semper 
 velit vitae elit ullamcorper id rutrum 

<ul class="image-info">
    <li id="ID9999-1" class="image-size"></li>
    <li id="ID9999-2" class="image-height"></li>
    <li id="ID9999-3" class="image-width"></li>
</ul>
<div id="ID9999-4"  class="file-preview"></div>
<div id="ID9999-5"  class="file-detail"></div>

In venenatis elementum mauris, 
at auctor nulla cursus ac. Duis accumsan nisi ut 
eros euismod vehicula. Duis luctus dui eget nibh 
tempus lacinia tristique libero imperdiet. 
Sed tristique pulvinar suscipit. 

通过php脚本,我想找到一个字符串模式 并用其他字符串替换它。
在下面你可以看到字符串模式 你可以在原始的原始字符串中找到它3次。

<ul class="image-info">
    <li id="unique-name1" class="image-size"></li>
    <li id="unique-name2" class="image-height"></li>
    <li id="unique-name3" class="image-width"></li>
</ul>
<div id="unique-name4"  class="file-preview"></div>
<div id="unique-name5"  class="file-detail"></div>

在原始字符串中,有很多这种模式字符串。
和“unique-name”是模式的标识id,唯一,ex)ID23291
这是替换字符串

<div id="unique--name" class="raw-image-info"></div>

所以,最终的字符串应该是这样的〜

Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
Cras a egestas lacus. Praesent non ante in dui pellentesque 
molestie. Fusce facilisis, libero eu pellentesque varius, 
est magna pretium urna, id dictum urna tortor eget neque. 
Sed a arcu a lectus euismod volutpat. Aenean est urna, 
ultricies quis consequat sit amet, pretium a massa. 
Praesent sit amet nisl nec dolor malesuada posuere. 
Phasellus ac urna eget ligula vehicula cursus. Donec
 volutpat dui egestas dolor aliquet interdum. Vestibulum 
 ante ipsum primis in faucibus orci luctus et ultrices 
 posuere cubilia Curae; Duis sed neque sed mauris gravida 
 rhoncus. 
<div id="ID5555" class="raw-image-info"></div>    
Cum sociis natoque penatibus et magnis dis parturient montes, 
nascetur ridiculus mus. Nam eu egestas ipsum. Duis ultrices 
vehicula metus, sit amet convallis libero suscipit ac.

<div id="ID7777" class="raw-image-info"></div>    

 Nullam libero lorem, suscipit in imperdiet ac, porttitor sit amet turpis.
 Cras tincidunt imperdiet eros, id sagittis felis pellentesque sit amet. 
 Mauris ultrices nulla ac dolor fermentum at feugiat erat egestas. Quisque 
 dapibus tellus eu dolor sagittis consequat. Maecenas dapibus dolor et nunc 
 sollicitudin laoreet. Sed lacus est, pharetra a fringilla eu, ornare varius 
 lacus. Etiam at lorem in leo molestie interdum ut vitae diam. Morbi semper 
 velit vitae elit ullamcorper id rutrum 

<div id="ID9999" class="raw-image-info"></div>    

In venenatis elementum mauris, 
at auctor nulla cursus ac. Duis accumsan nisi ut 
eros euismod vehicula. Duis luctus dui eget nibh 
tempus lacinia tristique libero imperdiet. 
Sed tristique pulvinar suscipit. 

我认为这有点复杂。正则表达式和preg_replace是这样的吗? 我是否必须找到另一种方法来完成这项工作?

2 个答案:

答案 0 :(得分:1)

Preg_replace()应该可以帮助您进行更换。

声明模式并替换它很容易。我需要花费一些时间来编写代码来获取unique_id并将其作为div id。目前我正在使用preg_replace()

粗略了解如何处理它
$pattern = '/\<ul class=\"image-info\"\>[^n]*class=\"file-detail\"\>\<\/div\>/';

$replace = '<div id="$unique_num" class="raw-image-info"></div>';

$string = "whatever may be string";

$string = preg_replace($pattern, $replace, $string);

这可以帮助您替换模式。我会在找到问题的完整解决方案后立即更新答案。希望这篇文章能为你的问题提供一些启示。

希望以下示例可以更好地指导您..我仍然需要找到获取unique_id的解决方案,但希望这会让您更有信心走向解决方案。 :)

$string = 'ante ipsum primis in faucibus orci luctus et ultrices  posuere cubilia Curae; Duis sed neque sed mauris gravida  rhoncus.  <ul class="image-info">     <li id=\"ID5555-1\" class=\"image-size\"></li>     <li id=\"ID5555-2\" class=\"image-height\"></li>     <li id=\"ID5555-3\" class=\"image-width\"></li> </ul> <div id=\"ID5555-4\"  class=\"file-preview\"></div> <div id=\"ID5555-5"  class="file-detail"></div> Cum sociis natoqu';

//echo $string;

$unique_num = 'ID5555';

$pattern = '/\<ul class=\"image-info\"\>[^n]*class=\"file-detail\"\>\<\/div\>/';


$replace = "<div id='".$unique_num."' class='raw-image-info'></div>";



$string = preg_replace($pattern, $replace, $string);

echo $string;

答案 1 :(得分:0)

最后,我找到了这个问题的解决方案。 我的解决方案是不使用php,使用jquery。 在客户端处理它,而不是服务器。

代码是这样的,

$(document).ready(function(){
    $("ul.image-info").each(function(){
        $(this).replaceWith('');
    });
    $("div.file-preview").each(function(){
        $(this).replaceWith('<div class="raw-image-info" id="' + 
          $(this).attr("id").substring(0, $(this).attr("id")-1) + '"></div>');
    });
    $("div.file-detail").each(function(){
        $(this).replaceWith('');
    });
});

希望这对另一个面对这类问题的人有所帮助。