Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

death to regular expressions! 5

Status
Not open for further replies.

cLFlaVA

Programmer
Jun 14, 2004
6,450
US
Hi all. I have content as follows, entered into a textarea:

Code:
blah blah blah <b>blah</b> blah blah.
<div id="code">
blah blah blah <b>blah</b> blah blah.
</div>
blah blah blah <b>blah</b> blah blah.

And, using, I guess preg_replace or eregi_replace, I'd like to return the following:

Code:
blah blah blah blah blah blah.
<div id="code">
blah blah blah &lt;b&gt;blah&lt;/b&gt; blah blah.
</div>
blah blah blah blah blah blah.

Can someone help me here? Thanks.

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
 
Hiya,

Give this a whirl, I forgot if my array context was right but it's just a matter of adjusting the replace string.
This is how you do it.

Code:
[blue]

$OldString = "<b>blah</b>";
$Replace_String = { "<b>","</b>" };
$New_String = "&lt;";
$MyNewString = str_replace($Replace_String,$New_String,$myNewString);
[green]// $MyNewString should equal "&lt;blah&lt"[/green]
[/blue]

Good Luck,
Ron

cout << "If you don't know where you want to go, we'll make sure you get taken";
 
I can't do it that way. One variable has the entire text of:

[tt]blah blah blah <b>blah</b> blah blah.
<div id="code">
blah blah blah <b>blah</b> blah blah.
</div>
blah blah blah <b>blah</b> blah blah.[/tt]

And I only want to replace < and > where they exist between <div class="code"> and </div>.

Besides, "blah" was just an example. Anything can be there.

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
 
cant you explode it, then run str_replace or strip_tags on the exploded part inside the div? (middle part)

eg. first you can explode it on <div id="code">
then you can explode it on </div>

then:
$foo = $foo[1] . strip_tags($foo[2]) . $foo[3];

ps. might be better to search with strpos and then run substr()

Olav Alexander Mjelde
Admin & Webmaster
 
Hmm, interesting thought DaButcher. I'll give it a try in a little bit and let you know what I come up with.

Thanks.

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
 
Bah!

Be more clear :p

-Ron

cout << "If you don't know where you want to go, we'll make sure you get taken";
 
how have I not been clear?

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
 
Here's a combination of explode() and htmlentities() that does what you want on the example string:
Code:
<?
$str = 'blah blah blah <b>blah</b> blah blah.
<div id="code">
blah blah blah <b>blah</b> blah blah.
</div>
blah blah blah <b>blah</b> blah blah.';
$x1 = explode('<div',$str,2);
$x2 = explode('</div',$x1[1],2);
$new_str = $x1[0].'<div'.htmlentities($x2[0]).'</div'.$x2[1];
?>

You would have to modify it to make the technique general.

Ken
 
DaButcher, Ken,

I'll try both of your solutions in about an hour. That's a personal project, and therefore not with me at work. I appreciate your help and will definitely get back to you.

Thanks again.

-Cory

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
 
p.s.:

will these solutions work with multiple instances of the code div? For example, something with this:

Code:
blah blah blah <b>blah</b> blah blah.
<div id="code">
blah blah blah <b>blah</b> blah blah.
</div>
blah blah blah <b>blah</b> blah blah.
blah blah blah <b>blah</b> blah blah.
<div id="code">
blah blah blah <b>blah</b> blah blah.
</div>
blah blah blah <b>blah</b> blah blah.

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
 
Hmm, if you make a function that either explodes or uses strpos and/or strrpos, you can put that inside a loop..

eg. loop while there are occurrances.

while (strpos(... > 0) {
$content = substr(...
}

I'm sorry that I cant provide you with more than this "tip", but I dont have access to ftp, ssh or anything else than :80 here.. (WEB). I might set up some webftp :p

I used to have an hosting account with cpanel, etc. so I could test code-samples I posted here.

But I think that you are skilled anyways, cflava, based on the help I've noticed that you have given to others on this forum.. So I guess you will be a-ok. with tips :)

Olav Alexander Mjelde
Admin & Webmaster
 
here is the RegEx option:
Code:
<?
$str="asdasdasdsda
<div id='code'>asda<c1>s</c>dasd</div>sd
<b>asda<a href=''>sd</a>as</b>
<div id='code'>a<a href=''>sd</a>sda<c>s</c>dasd</div>
";


preg_match_all("|<div id=['\"]code['\"]>.*<\/div>|Ui",$str,$StringToReplace);
for($i=0;$i<count($StringToReplace[0]);$i++)
{
	$TheString=preg_replace("/<div id=['\"]code['\"]>/i","",$StringToReplace[0][$i]);
	$TheString=preg_replace("/<\/div>/i","",$TheString);

	$TheString=str_replace("<","&lt;",$TheString);
	$TheString=str_replace(">","&gt;",$TheString);

	$TheString="<div id='code'>".$TheString;
	$TheString.="</div>";

	$str=eregi_replace($StringToReplace[0][$i],$TheString,$str);
}
echo $str;
?>

cLFlaVA:
RegExp are actually very very easy. Its just the attitude towards it that counts...



Known is handfull, Unknown is worldfull
 
Thanks to everyone. I didn't have much time last night to try this out, but will do so this evening. I appreciate your help as always.

vbkris-

i'm starting to see that. i was on a bunch of different sites two nights ago, and finally, the special characters (*+?) are starting to stick, as well as some of the logic behind reg ex's.

Thanks again everyone.

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
 
Here's a completely different thought:
Have you considered to manipulate the document using the DOM?
It can help you easily to get the different <div> tags and access their content.
 
If you're referring to JavaScript, I don't really think that's the best option.

I'm trying to set up a blogging utility on my own site, basically so I can just add my own entries whenever. I wanted to use either newly-created tags ([ignore]
Code:
[/ignore]) or tags like above (<div id="code"></div>). I know, I could just add whatever html I want, since it's my own site, but I wanted to make this as expandable as possible, in case I ever decide to use it on a professional level.

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
 
Wow, that seems pretty powerful, but way way over my head. There go any thoughts I had on my knowledge of PHP. Thanks for the link, though :) It says the document needs to be of XML variety... I don't even know much about XML.

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
 
XML variety just means it needs to be a tag based markup language, such as HTML is. There are two function to load specifically load HTML, for a file you'd use
Learning about the DOM and it's power is certainly worth the effort and I am sure you know about HTML.
 
So essentially I could use [tt]loadHTML( $htmlStringFromTextArea )[/tt], then use the [tt]getElementsByTagName()[/tt] method to retrieve all divs, then do what I need to do?

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top