Jump to content


Photo

how to replace these strings...


  • Please log in to reply
9 replies to this topic

#1 becu

becu
  • Members
  • PipPip
  • Member
  • 28 posts

Posted 12 August 2006 - 04:43 PM

Hi here's my problem:

<div align="center"><a href="link1">Text1</div>
Some text I want to keep.
<div align="center"><a href="link2">Text2</div>
Some other text I want to keep.
<div align="center"><a href="link3">Text3</div>
Some other text I want to keep.
<div align="center"><a href="link4">Text4</div>
Some other text I want to keep.

This is what I want, Output:
Some text I want to keep.
Some other text I want to keep.
Some other text I want to keep.
Some other text I want to keep.


I tried to use ereg_replace but it stripped out everything from the first <div align="center"> to the last </div><br/>.
Thank you so much for your help.

#2 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 12 August 2006 - 04:45 PM

I'm not 100% sure what you mean.. you just want it to output the text.. no links or anything?

#3 becu

becu
  • Members
  • PipPip
  • Member
  • 28 posts

Posted 12 August 2006 - 04:47 PM

I'm not 100% sure what you mean.. you just want it to output the text.. no links or anything?

yes, just those text in between. Or in other words, I want to get rid off all the div, a tags, and text1 -> text4

#4 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 12 August 2006 - 05:04 PM

You'd need to set those as variables then use the 'echo' command. Like this:

$Text1 = "blah";
$Text2 = "blah blah";
$Text3 = "blah blah blah";
$Text4 = "blah blah blah blah";


echo $Text1<br \>;
echo $Text2<br \>;
echo $Text3<br \>;
echo $Text4<br \>;

Now, the question is... how is that text getting placed into your HTML? Via a form input? In order for it to be extracted it needs to know how it got there. If it's posted via a form then the variables are set by the form field names using the $_POST command and your 'action' script parses the form input and displays the HTML and results of the form. Need a bit more detail on how this is created :)

#5 becu

becu
  • Members
  • PipPip
  • Member
  • 28 posts

Posted 12 August 2006 - 05:10 PM

No, I don't want those text1, text2, text3, text4.. .
I only want
"Some text I want to keep.
Some other text I want to keep.
Some other text I want to keep.
Some other text I want to keep."

A simple case of this which is:
$str = '<div align="center"><a href="link1">Text1</div>
Some text I want to keep.';
ereg_replace('<div align="center">(.*)</div>', '', $str);

output: Some text I want to keep.

but in this case I have more than one occurance of <div>, <a> tag, if I tried ereg_replace, it will take from the first <div align="center"> to the last </div> which will strip out all the text I want.

#6 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 12 August 2006 - 05:15 PM

What's wrong with what you said?
ereg_replace('<div align="center">(.*)</div>', '', $str);

Orio.
Think you're smarty?

(Gone until 20 to November)

#7 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 12 August 2006 - 05:16 PM

Ok, gotcha. I misunderstood your first post. I still would like to know the source of the HTML you're looking to strip. Is this coming via a database field or fields?

#8 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 12 August 2006 - 05:23 PM

My guess is it's being read from a file, no?

Btw, try this:
<?php
while(ereg("<div>",$str)!==FALSE){
$str=ereg_replace('<div align="center">(.*)</div>', '', $str);
}
echo $str;
?>

Orio.
Think you're smarty?

(Gone until 20 to November)

#9 becu

becu
  • Members
  • PipPip
  • Member
  • 28 posts

Posted 12 August 2006 - 06:10 PM

yes, i try to read from a file.
This is an error i get from orio code:
Fatal error: Maximum execution time of 30 seconds exceeded in...

in my first post, if I use
ereg_replace('<div align="center">(.*)</div>', '', $str);

output would be: "Some other text I want to keep" (the last text). all the other text (that I also want) in between will be stripped out.

Any idea?

#10 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 12 August 2006 - 06:17 PM

yea, when you read from the file, you can read by line and strip it out.

Read this: http://us3.php.net/file

It shows how to read each line.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users