0

Hi all

I'm trying to figure out how to go about finding all <a> tags in a string and replacing the spaces within the <a> tag with underscores.

Below is an example:

Before

<a href="path/to/page" class="pageLink">Page Name</a>

After

<a_href="path/to/page"_class="pageLink">Page_Name</a>

Any help would be much appreciated. Thanks.

5
Contributors
12
Replies
13
Views
7 Years
Discussion Span
Last Post by Venom Rush
Featured Replies
  • How about this for a one-liner: [CODE]echo preg_replace_callback('/<a ([^>]+)>([^<]*)<\/a>/m',create_function('$matches','return str_replace(" ","_",$matches[0]);'),$string);[/CODE] [B]BEFORE: [/B] Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed in ante nec nisl vulputate faucibus at quis <a href="path/to/page" class="pageLink">felis</a>. Aliquam vel dui orci. Integer eget eros sed lorem vestibulum aliquet. Aliquam tortor lorem, semper et rutrum … Read More

0
$string = str_replace("<a ","<a_",$string);

will search for "<a" in string and replace them with "<a_".

0

the above method will simply change the <a href... and put a _ after the first 'a' and thats it.

there may be a way to use an if command so where <a and > all the inclosed ' ' are replaced with'_'

all the help i can give atm

0

the above method will simply change the <a href... and put a _ after the first 'a' and thats it.

there may be a way to use an if command so where <a and > all the inclosed ' ' are replaced with'_'

all the help i can give atm

That's what I've been trying to come up with. Not having any luck atm o.O

0
$string = preg_replace('/\s/', '_', $string );
// will replace "<a href="path/to/page" class="pageLink">Page Name</a>"
// width "<a_href="path/to/page"_class="pageLink">Page_Name</a>"
0
$string = preg_replace('/\s/', '_', $string );
// will replace "<a href="path/to/page" class="pageLink">Page Name</a>"
// width "<a_href="path/to/page"_class="pageLink">Page_Name</a>"

That's all fine and well but how do I go about replacing the spaces in every link inside a body of copy if I have the following:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed in ante nec nisl vulputate faucibus at quis <a href="path/to/page" class="pageLink">felis</a>. Aliquam vel dui orci. Integer eget eros sed lorem vestibulum aliquet. Aliquam tortor lorem, semper et rutrum a, hendrerit non tellus. Donec <a href="path/to/page" class="pageLink">sodales nunc</a> sed justo viverra luctus. Proin magna lectus, posuere sed laoreet dapibus, mattis at odio. Fusce sed ultricies augue. Curabitur id eros eu turpis imperdiet vestibulum. Nam <a href="path/to/page" class="pageLink">vitae ante dolor</a>, et placerat elit. Suspendisse nec dictum lectus. Morbi nisi nunc, porta nec tempor non, mollis sed justo. Sed pulvinar commodo consectetur.
0

Ok, the url is in a block of text. Can't do it in a one liner:

<?php

	$string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed in ante nec nisl vulputate faucibus at quis <a href=\"path/to/page\" class=\"pageLink\">felis</a>. Aliquam vel dui orci. Integer eget eros sed lorem vestibulum aliquet. Aliquam tortor lorem, semper et rutrum a, hendrerit non tellus. Donec <a href=\"path/to/page\" class=\"pageLink\">sodales nunc</a> sed justo viverra luctus. Proin magna lectus, posuere sed laoreet dapibus, mattis at odio. Fusce sed ultricies augue. Curabitur id eros eu turpis imperdiet vestibulum. Nam <a href=\"path/to/page\" class=\"pageLink\">vitae ante dolor</a>, et placerat elit. Suspendisse nec dictum lectus. Morbi nisi nunc, porta nec tempor non, mollis sed justo. Sed pulvinar commodo consectetur.";

	$x = split("<a",$string);
	$string = "";

	foreach($x as $elem) {
		if (preg_match("/^ href(.*)/",$elem)) $elem = "<a".preg_replace('/\s/', '_', $elem)."<";
		$string .= $elem;
	}

	echo $string."\n";
?>

The output of this will be:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed in ante nec nisl vulputate faucibus at quis <a_href="path/to/page"_class="pageLink">felis</a>. Aliquam vel dui orci. Integer eget eros sed lorem vestibulum aliquet. Aliquam tortor lorem, semper et rutrum a, hendrerit non tellus. Donec <a_href="path/to/page"_class="pageLink">sodales_nunc</a> sed justo viverra luctus. Proin magna lectus, posuere sed laoreet dapibus, mattis at odio. Fusce sed ultricies augue. Curabitur id eros eu turpis imperdiet vestibulum. Nam <a_href="path/to/page"_class="pageLink">vitae_ante_dolor</a>, et placerat elit. Suspendisse nec dictum lectus. Morbi nisi nunc, porta nec tempor non, mollis sed justo. Sed pulvinar commodo consectetur.

Edited by colweb: Didn't copy the whole thing

2

How about this for a one-liner:

echo preg_replace_callback('/<a ([^>]+)>([^<]*)<\/a>/m',create_function('$matches','return str_replace(" ","_",$matches[0]);'),$string);

BEFORE:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed in ante nec nisl vulputate faucibus at quis <a href="path/to/page" class="pageLink">felis</a>. Aliquam vel dui orci. Integer eget eros sed lorem vestibulum aliquet. Aliquam tortor lorem, semper et rutrum a, hendrerit non tellus. Donec <a href="path/to/page" class="pageLink">sodales nunc</a> sed justo viverra luctus. Proin magna lectus, posuere sed laoreet dapibus, mattis at odio. Fusce sed ultricies augue. Curabitur id eros eu turpis imperdiet vestibulum. Nam <a href="path/to/page" class="pageLink">vitae ante dolor</a>, et placerat elit. Suspendisse nec dictum lectus. Morbi nisi nunc, porta nec tempor non, mollis sed justo. Sed pulvinar commodo consectetur.

AFTER:

<a_href="path/to/page"_class="pageLink">felis</a>. Aliquam vel dui orci. Integer eget eros sed lorem vestibulum aliquet. Aliquam tortor lorem, semper et rutrum a, hendrerit non tellus. Donec <a_href="path/to/page"_class="pageLink">sodales_nunc</a> sed justo viverra luctus. Proin magna lectus, posuere sed laoreet dapibus, mattis at odio. Fusce sed ultricies augue. Curabitur id eros eu turpis imperdiet vestibulum. Nam <a_href="path/to/page"_class="pageLink">vitae_ante_dolor</a>, et placerat elit. Suspendisse nec dictum lectus. Morbi nisi nunc, porta nec tempor non, mollis sed justo. Sed pulvinar commodo consectetur.

Votes + Comments
Brilliant! Thanks for the help ;)
Perfect example of a reg expression in action.
0

How about this for a one-liner:

echo preg_replace_callback('/<a ([^>]+)>([^<]*)<\/a>/m',create_function('$matches','return str_replace(" ","_",$matches[0]);'),$string);

That's a nice one. But where is "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed in ante nec nisl vulputate faucibus at quis " ?

0

He must have copy and pasted wrong. It works...

Err - yes sorry copy/paste error :) I only pasted some of the code (it was 2 am when I did this!) As you said, it does indeed work - I just tried it again just to check.

Edited by edwinhermann: n/a

0

How about this for a one-liner:

echo preg_replace_callback('/<a ([^>]+)>([^<]*)<\/a>/m',create_function('$matches','return str_replace(" ","_",$matches[0]);'),$string);

BEFORE:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed in ante nec nisl vulputate faucibus at quis <a href="path/to/page" class="pageLink">felis</a>. Aliquam vel dui orci. Integer eget eros sed lorem vestibulum aliquet. Aliquam tortor lorem, semper et rutrum a, hendrerit non tellus. Donec <a href="path/to/page" class="pageLink">sodales nunc</a> sed justo viverra luctus. Proin magna lectus, posuere sed laoreet dapibus, mattis at odio. Fusce sed ultricies augue. Curabitur id eros eu turpis imperdiet vestibulum. Nam <a href="path/to/page" class="pageLink">vitae ante dolor</a>, et placerat elit. Suspendisse nec dictum lectus. Morbi nisi nunc, porta nec tempor non, mollis sed justo. Sed pulvinar commodo consectetur.

AFTER:

<a_href="path/to/page"_class="pageLink">felis</a>. Aliquam vel dui orci. Integer eget eros sed lorem vestibulum aliquet. Aliquam tortor lorem, semper et rutrum a, hendrerit non tellus. Donec <a_href="path/to/page"_class="pageLink">sodales_nunc</a> sed justo viverra luctus. Proin magna lectus, posuere sed laoreet dapibus, mattis at odio. Fusce sed ultricies augue. Curabitur id eros eu turpis imperdiet vestibulum. Nam <a_href="path/to/page"_class="pageLink">vitae_ante_dolor</a>, et placerat elit. Suspendisse nec dictum lectus. Morbi nisi nunc, porta nec tempor non, mollis sed justo. Sed pulvinar commodo consectetur.

Thanks edwinhermann

This works like a charm! You rock ;)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.