0

I currently have a string in PHP that I need to manipulate.

I cannot modify the back-end code, I can only work with the output.

Currently the string I want to modify is a series of links, in this format:

<a href="somepage.php">some title</a><a href="somepage2.php">some other title</a><a href="somepage3.php">another title</a>

To work with a script I am using I need to add a z-index value to each link, in increasing values. So, in my example above, the code needs to end up looking like this:

<a href="somepage.php" style="z-index:1">some title</a><a href="somepage2.php" style="z-index:2">some other title</a><a href="somepage3.php" style="z-index:3">another title</a>

I know how to replace part of a string using str_replace, so if all of the links were using the same z-index value I could search for all cases of <a href and replace it with <a style="z-index:1" href and it would solve my problem, but each link needs a different z-index value.

So what is the most efficient way to take a string containing multiple links, and add the necessary 'style' tag and z-index values to each one?

EDIT

I should also add that once the z-index values are added the links all need to be joined into one string again.

3
  • Can you make array of all the anchor tags and its data? Commented Oct 31, 2013 at 4:52
  • I guess I can do whatever I want with the string. Do you have any ideas? Commented Oct 31, 2013 at 4:55
  • do you have an array of urls or is it a string that has the links in there? Commented Oct 31, 2013 at 5:37

3 Answers 3

2
<?php
$src_str = '<a href="somepage.php">some title</a><a href="somepage2.php">some other title</a><a href="somepage3.php">another title</a>';
$str_list = explode('</a>', $src_str);

$result = '';

$count = 0;
foreach ($str_list as $item)
{
    if (empty($item))
    {
        continue;
    }
    list($part1, $part2) = explode('>', $item);

    $count++;
    $result .= $part1 . " style=\"z-index:$count\">" . $part2 . '</a>';
}
echo $result;

// output:
// <a href="somepage.php" style="z-index:1">some title</a>
// <a href="somepage2.php" style="z-index:2">some other title</a>
// <a href="somepage3.php" style="z-index:3">another title</a>
Sign up to request clarification or add additional context in comments.

Comments

1
$link = $('a[href]');

$link.each(function(k,v){
 $(v).css('z-index',some_value);
});

Comments

0

You should simply use jQuery to modify your css. Something like that :

$(a[href='your-link.php']).css("z-index", "value");

6 Comments

How would I be able to do this on a string containing multiple links, when each needs a different z-index value, and the URLs in the string can be any value and not always the same?
Hum, your jQuery can be dynamic. Where do you get your string from ? A database?
He tagged PHP not jQuery. :)
The string comes from the back-end system, which has encoded files, so I cannot change the source. I get a variable, say $links, with a string of all of the links.
I would also prefer to have this done right in the source code, so PHP would be better.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.