Jump to content

Archived

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

Guest Thunderhawk66

Lines

Recommended Posts

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

For example:

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

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.

Share this post


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

Share this post


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

Share this post


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

[code]$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++;
[/code]

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

Share this post


Link to post
Share on other sites
Guest Thunderhawk66
Can someone help?
Thanks.

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.