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.

Recommended Answers

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

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

Jump to Post

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 …

Jump to Post

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, …

Jump to Post

All 12 Replies

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

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

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

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

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

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.

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.

commented: Perfect example of a reg expression in action. +3
commented: Brilliant! Thanks for the help ;) +3

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 " ?

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

edwinhermann fantastic one-liner. Did try it and it works perfectly. :cool:

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.