开发者

Find string pattern and replace it~

I have a bunch of html string.(Raw String)

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. 

Through php script, I want to find a string pattern and replace it with some other string.

below you can see the string pattern.

and you can find it 3 times in the original raw string.

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

in the raw string, there are lots of this pattern strings.

and "unique-name" is identification id of the pattern, unique, ex)ID23291

and this is replacement string

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

so, the final string should be like this~

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. 

I think it's a little bit complicate. regex and preg_replace is the way?

Do I have to find another way to do this work?


Preg_replace() should help you to do the replacement.

It will be easy for declaring the pattern and replace it. It will take some more time for me to write the code to get the unique_id and place it as the div id. For the time being I am getting you a rough idea about how to handle it using 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);

This should help you to replace the pattern. I will be updating the answer as soon as I find the complete solution for the issue. Hope this post will shed some light for your issue.

Hope the following example may guide you better.. Still I have to find a solution for getting the unique_id, but hope this will give you more confidence to walk towards solution. :)

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


At last, I find a solution of this problem. My solution is not using php, using jquery. Process it in client, not server.

the code is like this,

$(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('');
    });
});

Hope this help for another guys who confront this kind of problem too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜