We help IT Professionals succeed at work.

.append link inside a <H4> while in a php loop

helpchrisplz
helpchrisplz asked
on
509 Views
Last Modified: 2014-01-09
Hi all

i have a php foreach loop that outputs html to the page with every loop.

i have a variable that stores the html inside it. this variable gets echo out on each loop.

what i want to do is update this variable (before it gets eco out) so that it has its old heading removed and update the contents of the heading to have an <a href> inside it.

The <a href> is also available as a variable with in the loop.

example: this is the output of the variable with <xmp> tags surrounding it

	<div class="lesson-desc-inner top-cat">
                  <div class="w-row">
                    <div class="w-col w-col-4 center">
                      <img class="pad-image" src="/sites/the_english_notebook/themes/theme_by_chris_sowerby/images/book%20icon.png" width="92" alt="52c6d55a9c7b16355f0002bd_book%20icon.png">
                    </div>
                    <div class="w-col w-col-8">
                      <div class="lesson-select-text-area">
                        <h4 class="reusable-heading">Module name will update...</h4>
                        <div class="text-contain">
                          <p>In the 2nd grade you learned to add and subtract 2-digit numbers. Now we take things further by adding and subtracting three-digit numbers. This is good practice for 4th grade where you&#039;ll be expected to add or subtract pretty much any whole number!</p>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>

Open in new window


so i need to update this part:

<h4 class="reusable-heading">Module name will update...</h4>

Open in new window



to look like this:


<h4 class="reusable-heading">
<a href="/sites/the_english_notebook/index.php/pick-your-key-stage/key-stage-1/verbs-2/" target="_self">verbs 3</a>
</h4>

Open in new window


the <a href> can be  output within my loop like this:

<a href='<?php  echo $url ?>' target='<?php  echo $target ?>'><?php  echo $title ?></a>

Open in new window



so i just need to replace the old heading with the link but am not sure how i can break apart my html from its variable and add my link variables inside it at the correct places.

Thanks for helping.
Comment
Watch Question

gr8gonzoConsultant
CERTIFIED EXPERT

Commented:
What does your foreach code look like?

Author

Commented:
like this:

<?php  foreach ($pages as $page):
	$title = $th->entities($page->getCollectionName());
	$url = $nh->getLinkToCollection($page);
	$target = ($page->getCollectionPointerExternalLink() != '' && $page->openCollectionPointerExternalLinkInNewWindow()) ? '_blank' : $page->getAttribute('nav_target');
	$target = empty($target) ? '_self' : $target;
?>

<?php $teaser = $plth->getPageTeaser($page, 'Description of this lesson', $teaserBlockCount, $teaserTruncateChars);		?>
	
	<a href="<?php  echo $url; ?>" target="<?php  echo $target; ?>"><?php  echo $title; ?></a>


	<?php  echo $teaser; // html code output here ?>
	
<?php  endforeach; ?>

Open in new window

Consultant
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
yes that did it :) thanks

looks like i really need to learn how preg_replace works.

This baffles me:

 ("@(<h4 class=\"reusable-heading\">)([^<]+)(</h4>)@","\\1{$link}\\3",$teaser)
gr8gonzoConsultant
CERTIFIED EXPERT

Commented:
It starts with the syntax:

RESULT = preg_replace( FIND REGULAR EXPRESSION,  REPLACE WITH,  ORIGINAL CONTENT );

So:

$teaser = preg_replace("/e/","a", $teaser);

...would change every "e" with an "a".

Regular expressions need delimiters to indicate when they should start and stop, which is why there are / around the "e". The delimiter can be just about any non-alpha-numeric character, so you could use @ or %, for example:

$teaser = preg_replace("/e/","a", $teaser);
$teaser = preg_replace("%e%","a", $teaser);
$teaser = preg_replace("@e@","a", $teaser);

All three lines do the exact same thing. The / is simply the most common version. People use different delimiters to avoid having to escape characters in their regular express that also happen to be the delimiter. For example, if you wanted to change all instances of "/h4>" to the word "blah", you might think of doing this:

$teaser = preg_replace("/</h4>/","blah",$teaser);

...however that / right before h4 is also the regular expression delimiter, so this would throw an error because the regular expression engine doesn't know whether you're trying to end the regular expression or if you're looking for the / character. So you can either escape the / with a backslash \ or you can use a different delimiter:

$teaser = preg_replace("/<\/h4>/","blah",$teaser);
or
$teaser = preg_replace("@</h4>@","blah",$teaser);

When I'm working with HTML, I usually use the @ delimiter so I don't have to keep escaping every forward slash character.

Now, when you use parentheses, you're "capturing" that content temporarily and you can use it in your output. For example:

$teaser = "The quick brown fox.";
$teaser = preg_replace("@(quick) (brown) (fox)@", "\\3, which is \\1 and \\2", $teaser);

The first word "quick" is capture number 1, "brown" is capture number 2, and "fox" is capture number 3. In the second part of preg_replace, you see \\1, which means "the value of capture 1", \\2 which means "the value of capture 2", and \\3, which means "the value of capture 3". So the above would result in $teaser being "The fox, which is quick and brown.";

Finally, the last "magic" piece of the regex was this: [^<]+. Put simply, it means "every character that ISN'T the < character. So if I had this:

$teaser = "The quick brown fox.";
$teaser = preg_replace("@(quick) ([^n]+n) (fox)@", "\\3, which is \\1 and \\2", $teaser);

... I would still get the same result as before. The regular expression in the middle says "any character except for n". The regular expression goes character-by-character. It starts with "b", which is not "n", so it gets added. Then "r", which is not "n", so it also gets added, and so on. It finally gets to the point where "brow" is captured, and then the regular expression sees an "n", so it stops. However, since I want the "n", too, I have it as part of my capture, so the second capture group is really two expressions: [^n]+ and then the letter "n". Together, you get "brown". This isn't the most useful regular expression with that phrase, but in HTML, it's a pretty simple way sometimes to get the inner content of a tag, because it's matching everything EXCEPT for the beginning of the next tag, which starts with <. So:

$teaser = "Hello <sometag>World</sometag>!";
$teaser = preg_replace("@<sometag>([^<]+)</sometag>@", "\\1", $teaser);

...would result in "Hello World!"

So what I did with your content is capture the whole <h4> starting tag as capture #1, all of the content leading up to the </h4> ending tag as capture #2, and then the </h4> ending tag itself as capture #3. Then I replaced it all with capture #1 (the starting H4 tag), followed by the link HTML, followed by capture #3 (the ending H4 tag).

I didn't need to capture the content of your H4 tag as capture #2, but I wasn't sure if you were going to use it, so I figure it would be easier to write the more "complete" regex up front and if you wanted to reuse the content somehow (like as the link text), you could just throw \\2 in there somewhere.

Hopefully that all makes sense. Once you know regular expressions well, this type of stuff isn't very difficult. It looks FAR more complicated than you think it is. I also use a free tool called The Regex Coach all the time to help me see what my regular expressions are doing as I write them.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.