Jump to content

[SOLVED] not sure if preg_replace is what I need


tryingtolearn

Recommended Posts

Hello,

I started this post over on the help board before I realized this board was even here.

I hope someone can help with this or even let me know if its possible.

 

I am totally confused over this preg_replace

Basically I am not sure what function I should be using to accomplish something.

 

What I need to do is convert everything between multiple html comment tags so they do not get stripped when using strip tags.( might not be an img tag in there either - so what ever is between the two comment tags I need in a format that wont get touched by the strip_tags)

 

So if I start with this

 

 

Bunch of html code

<!-- STARTMARK --><img src="graphics/pic1.gif" width="40" height="40" alt="mypic1" border="0"><!-- ENDMARK -->

<img src="graphics/pic2.gif" width="40" height="40" alt="mypic2" border="0">

<!-- STARTMARK --><img src="graphics/pic3.gif" width="40" height="40" alt="mypic3" border="0"><!-- ENDMARK -->

Bunch of html code

 

 

It will change to something like this

 

Bunch of html code

<!-- STARTMARK --><img src="graphics/pic1.gif" width="40" height="40" alt="mypic1" border="0"><!-- ENDMARK -->

<img src="graphics/pic2.gif" width="40" height="40" alt="mypic2" border="0">

<!-- STARTMARK --><img src="graphics/pic3.gif" width="40" height="40" alt="mypic3" border="0"><!-- ENDMARK -->


Bunch of html code

 

So I can run it through strip tags and end up with this back on the html page.

 

 

 

 

Bunch of text

<!-- STARTMARK --><img src="graphics/pic1.gif" width="40" height="40" alt="mypic1" border="0"><!-- ENDMARK -->


<!-- STARTMARK --><img src="graphics/pic3.gif" width="40" height="40" alt="mypic3" border="0"><!-- ENDMARK -->



Bunch of text

 

There can be any # of the STARTMARK and ENDMARK tags from 0-100 it will just depend on the page.

 

I have everything but converting only those marked sections

Can this be done?

 

Im this far but am lost when it comes to replacing everything in the middle of the tags.

 

$search = array ("'<!-- STARTMARK --[^>]*?>.*?<!-- ENDMARK -->'si");
$replace = array ("<!-- STARTMARK -->.*<!-- ENDMARK -->"); 
$new = preg_replace ($search, $replace, $source);

 

Any ideas???

Well I dont know how but I tried this and it works but there are two issues

 

1.  It seems like a crude way of doing it (Compared to what effigy was saying with the callback)

2.  It only seems to work with an image tag

I had to replace the <img to get the contents back - an imag in this example.

 

$search2 = array ("'<!-- STARTMARK -->[<]([a-zA-Z]|[0-9])*'");
$replace2 = array ("IfR!-- STARTMARK --IfR XXX "); 
$strip2 = preg_replace ($search2, $replace2, $strip);
$newcode =  strip_tags($strip2, '<p>,<br>,<H1>');
$search3 = array ("'IfR!-- STARTMAK --IfR'","'XXX'");
$replace3 = array ("<!-- STARTMARK -->","<img"); 
$strip3 = preg_replace ($search3, $replace3, $newcode);

 

This is very confusing.

Actually, the /e modifier would be easier. ( )'s are used to capture data, which can be accessed via backreferences. See the links in my signature for more info.

 

<pre>
<?php
$data = <<<DATA
Bunch of html code

<!-- STARTMARK --><img src="graphics/pic1.gif" width="40" height="40" alt="mypic1" border="0"><!-- ENDMARK -->

<img src="graphics/pic2.gif" width="40" height="40" alt="mypic2" border="0">

<!-- STARTMARK --><img src="graphics/pic3.gif" width="40" height="40" alt="mypic3" border="0"><!-- ENDMARK -->

Bunch of html code	
DATA;

echo $data, '<hr>';
$data = preg_replace('/<!-- STARTMARK -->(.+?)<!-- ENDMARK -->/se', 'htmlentities("\1")', $data);
echo $data;
?>
</pre>

Thanks effigy, I will look at in depth when I get back this evening.

 

I spent a loooong time at those links last night.

They are helpful it just takes me a really long time to get this straight.

Wow -

 

Thanks for your time.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.