Jump to content


Lines


  • Please log in to reply
5 replies to this topic

#1 Guest_Thunderhawk66_*

Guest_Thunderhawk66_*
  • Guests

Posted 29 April 2006 - 01:53 AM

I have a script that uses a Regular Expression to get data from a textarea.

For example:

preg_match_all("/Address:.[^\\n]*/i", $str, $matches);
foreach ($matches as $val) {
echo $val[1];
}

but I was wondering if there is a way to print a line of text two lines above it?

Eg if there is this text:

Hello
Blah
Address: 123

and the code prints "Address: 123", how would I get it to print the text two lines above it ("Hello")?

Thanks,
-Thunderhawk66.

#2 Twentyoneth

Twentyoneth
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts
  • LocationGlasgow, KY

Posted 29 April 2006 - 02:30 AM

echo "Hello<br>Blah<br>" . $val[1];


#3 Guest_Thunderhawk66_*

Guest_Thunderhawk66_*
  • Guests

Posted 29 April 2006 - 02:54 AM

Thanks for that, but I meant if a user entered text in the textarea, and I found some text called "Address:", could I print the second line that was above it (if I didn't know what was entered).
That "Hello, Blah" was an example of what could be entered in the textarea.

#4 Orio

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

Posted 29 April 2006 - 12:46 PM

Use the nl2br function, then use the explode function (split by "<br />").
Then find the part with the "Address: " (lets say it was line in "n"), and echo $var[n] and $var[n-2].

Orio.
Think you're smarty?

(Gone until 20 to November)

#5 Guest_Thunderhawk66_*

Guest_Thunderhawk66_*
  • Guests

Posted 29 April 2006 - 09:56 PM

That sounds like it should work, thanks.
But could you maybe give an example?
I don't understand what you mean by "lets say it was line in "n"".
Here's some of the code I have:

$str = nl2br(rtrim($_POST[text]));
$num = 0;
$i = 0;
while ($i < 200)
{
preg_match_all("/Address:.[^\\n]*/i", $str, $matches);
foreach ($matches as $val) {
$txtol = $val[$num];
$expl = explode("<br />", $txtol);
echo $expl[$num-2];
}
}
$i++;
$num++;

Is that the way I do it?
Thanks for your help.

#6 Guest_Thunderhawk66_*

Guest_Thunderhawk66_*
  • Guests

Posted 01 May 2006 - 05:37 AM

Can someone help?
Thanks.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users