Parsing out a string and imploding back together to display original and parsed string.
I have something like:
$ads_raw='Sample link <a href="sample.html">sample 1</a>.
Sample link <a href="sample.html">sample 2</a>.
Sample link <a href="sample.html">sample 3</a>.';
I've exploded and counted them (because I needed to count them for another purpose):
for ($i=0;$i<$number_lines;$i++)
$lines[$i] = preg_replace('/(.*?)<a href="(.*?)">(.*?)<\/a>\./is', '$3 link <a href="$2">$3</a>.<br>$2<br><br>', $lines[$i]);
print_r($lines);
?>
Which gives the output:
Array
(
[0] => Sample link <a href="sample.html">sample 1</a>.
[1] => Sample link <a href="sample.html">sample 2</a>.
[2] => Sample link <a href="sample.html">sample 3</a>.
)
Array
(
[0] => sample 1 link <a href="sample.html">sample 1</a>.<br>sample.html<br><br>
[1] => sample 2 link <a href="sample.html">sample 2</a>.<br>sample.html<br><br>
[2] => sample 3 link <a href="sample.html">sample 3</a>.<br>sample.html<br><br>
)
Chris Andrews
ASKER
Thank you, that almost does the trick... just one more thing....
How do I get that second array to print out without the [1] => in front of each? I'll experiment while awaiting your answer, I may be able to figure it out,
I'm having some trouble, drove me crazy for a while trying to figure out why this worked in the example but not in practice, finally figured out that (I think) the preg_replace is too narrowly designed.
For example, if I have the sentences changed a bit, like:
$ads_raw='<a href="sample1.html">Sample 1</a> is the first link.
The second is <a href="sample2.html">sample 2</a>.
And <a href="sample3.html">sample 3</a> is the third.';
I end up with results like (notice second array not formatting correctly):
Array
(
[0] => <a href="sample1.html">Sample 1</a> is the first link.
[1] => The second is <a href="sample2.html">sample 2</a>.
[2] => And <a href="sample3.html">sample 3</a> is the third.
)
Array
(
[0] => <a href="sample1.html">Sample 1</a> is the first link.
[1] => sample 2 link <a href="sample2.html">sample 2</a>.<br>sample2.html<br><br>
[2] => And <a href="sample3.html">sample 3</a> is the third.
)
I'm not sure how this search expression stuff works, so I'm not sure how to fix this myself. I would ask this in a new question but my credit card company has decided not to accept charges from ee, they think ee is fraudelent, even though I've been using them for years. So now my account has a status of 'limited access'. Grrr. I'm trying to get this issue fixed, but for now, the points I gave you were all I had......
Can you show how you want the new example to output?
Chris Andrews
ASKER
Like the previous, it will need to display the line as is, then a <br>, then the url in the link, then <br><br>. So the results should look like:
[0] => <a href="sample1.html">Sample 1</a> is the first link.<br>sample1.html<br><br>
[1] => The second is <a href="sample2.html">sample 2</a>.<br>sample2.html<br><br>
[2] => And <a href="sample3.html">sample 3</a> is the third.<br>sample3.html<br><br>
I'm sorry if that was unclear earlier. There will only be one link per line. But the link will be in different places in the sentence. At the end of the sentance there may or may not be a period (may be an exclamation or question mark or nothing).
Chris Andrews
ASKER
The only double quote marks on the line will be surrounding the url, so whatever is between the double quote marks is the url that needs to be displayed under the line, if that makes it easier....
$ads_raw='<a href="sample1.html">Sample 1</a> is the first link.
The second is <a href="sample2.html">sample 2</a>.
And <a href="sample3.html">sample 3</a> is the third.';
Array
(
[0] => <a href="sample1.html">Sample 1</a> is the first link.
[1] => The second is <a href="sample2.html">sample 2</a>.<br>sample2.html<br><br>
[2] => And <a href="sample3.html">sample 3</a> is the third.
)
so [1] is correct, with the breaks and url, but the others did not come out correctly.
Thanks again, Chris
Chris Andrews
ASKER
Ncoo,
I got it figured out :)
Thank you for all your help, you went above and beyond the call. I will keep an eye out for you in the future.
http://uk2.php.net/function.preg_replace
<?php
$ads_raw='Sample link <a href="sample.html">sample 1</a>.
Sample link <a href="sample.html">sample 2</a>.
Sample link <a href="sample.html">sample 3</a>.';
$lines = explode("\n", $ads_raw);
$number_lines = count($lines);
print_r($lines);
for ($i=0;$i<$number_lines;$i+
$lines[$i] = preg_replace('/(.*?)<a href="(.*?)">(.*?)<\/a>\./
print_r($lines);
?>
That code will do what you want