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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

searching a file line by line and appending strings 2

Status
Not open for further replies.

chineerat

Technical User
Oct 12, 2003
42
TT
hi!i have a "foreach" loop which i am using to go thur a html file, line by line

eg.

$content= file("html_file.html");
$lines = $content;
foreach ($lines as $line){

}
however, i would like to check each line for a string
for example "<br>" and the exit condition is when "/br>" is present.

do i use an if loop or "is_string"???

in addition to this, how do i get the contents within "<br>" and "</br>" to be appended to a string.

i have tried $appended_string = $appended_string.$appended_string;

thanks in advnace...

 
To clarify, what exactly do you want to do? Pull out the strings between the <br> tags, and then put then all into a single string???

I'm pretty sure you'll need to use a regular expressions. Since i'm not very good at them, maybe someone else here can chime in with an example. If not, there are plenty of regex threads here, i'm sure you can find an example.

As for the appending, usually something like thsi works:

Code:
$myvar="A string...";
$somemorecontent=" with appended content.";

$myvar.=$somemorecontent;

will yield: "A string... with appended content."





----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
to test for the presence of a string within a string

Code:
if (strpos($line, "/br>") > 1) {
 //string is present
 break(); //causes php to break out of the loop
}

to retrieve the information between tags
Code:
$pattern = '/<br>(.*?)</br>/i';
preg_match_all($pattern, $line, $matches);
print_r ($matches);

for the appending, vacunita has given you the answer.
 
in response to vacunita
"
To clarify, what exactly do you want to do? Pull out the strings between the <br> tags, and then put then all into a single string??? "
yes, dat is exactly what i want to do.
thanks!

in response to jpadie,

$pattern = '/<br>(.*?)</br>/i'; is giving the error:

Warning: preg_match_all() [function.preg-match-all]: Unknown modifier 'b' in c:\inetpub\
is it because of the "/" present before br been mistaken for the "/" before "i".


thanks both for you for your help.
 
forgot to escape ...
Code:
$pattern = '/<br>(.*?)<[red]\[/red]/br>/i';
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top