Jump to content

Archived

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

becu

how to replace these strings...

Recommended Posts

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.

Share this post


Link to post
Share on other sites
I'm not 100% sure what you mean.. you just want it to output the text.. no links or anything?

Share this post


Link to post
Share on other sites
[quote author=Jocka link=topic=103999.msg414602#msg414602 date=1155401149]
I'm not 100% sure what you mean.. you just want it to output the text.. no links or anything?
[/quote]
yes, just those text in between. Or in other words, I want to get rid off all the div, a tags, and text1 -> text4

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
What's wrong with what you said?
ereg_replace('<div align="center">(.*)</div>', '', $str);

Orio.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
My guess is it's being read from a file, no?

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

Orio.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

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.