Avatar of Chris Andrews
Chris Andrews
Flag for United States of America asked on

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):

$lines = explode("\n", $ads_raw);
$number_lines = count($lines);

Now I need to parse out the url, then implode everything back together with the url below the original line, and add in breaks, so I end up with:

Sample 1 link <a href="sample1.html">sample 1</a>.<br>
sample1.html<br><br>

Sample 2 link <a href="sample2.html">sample 1</a>.<br>
sample2.html<br><br>

Sample 3 link <a href="sample3.html">sample 1</a>.<br>
sample3.html<br><br>

Maybe a 'foreach' statement?  Don't have any idea how to write that.  The ultimate purpose being to easily proof that the correct urls are being used.

Thanks,        Chris   (using php 4.3.3)
PHP

Avatar of undefined
Last Comment
ncoo

8/22/2022 - Mon
ncoo

You will need to use a preg_replace.

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>\./is', '$3 link <a href="$2">$3</a>.<br>$3<br><br>', $lines[$i]);

print_r($lines);
?>

That code will do what you want
ncoo

Sorry that should have been:

<?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>\./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,

Chris
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
ncoo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Chris Andrews

ASKER
Thank you very much!

Chris
ncoo

No problem, thank you.
Chris Andrews

ASKER
Ncoo, if you're still around....

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......

Thank you,  Chris
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ncoo

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....
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ncoo

Try this, let me know if this is what you mean.

$lines[$i] = preg_replace('/(.*?)<a href="(.*?)">(.*?)<\/a>\./is', '$1 <a href="$2">$3</a>.<br>$2<br><br>', $lines[$i]);
Chris Andrews

ASKER

This is what I am running:

$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.';

$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>\./is', '$1 <a href="$2">$3</a>.<br>$2<br><br>', $lines[$i]);
print_r($lines);

and the results are:

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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ncoo

Thanks St Aug Beach Bum. preg_replace's are great once you get the hang of them.