0

I have a link that looks like this:
<a href="/site/cdb/index.php?adinfo=272">Company Inc.</a>

I want to split it so I remain with two pieces of information, the value of adinfo, in the example 272, and the linking words, in the example Company Inc..

I can do it with the code below but that is six lines of code which chops the string in to bits and I find it quite ugly, so I was wondering if there is an easier/nicer way.

$str = right($str, strlen($str)-36);
$pos = strpos($str, '"');
$value = left($str, $pos);
$str = right($str, strlen($str)-($pos+2));
$pos = strpos($str, '</a>');
$link = left($str, $pos);

(left and right are functions of my own that keep the left or right part of a string and cut of the rest)

2
  • I think a regular expression would be the way for you, however I'm not good enough at them to write one for you.
    – Travesty3
    Mar 8, 2012 at 16:31
  • This does a lot more than you asked for, so the overhead might not be worth it. (I didn't look at the source) But it should help you do this, and any other HTML parsing you need to do. I can't wait to try it out too. Simple HTML DOM
    – TecBrat
    Mar 8, 2012 at 16:46

3 Answers 3

2

You shouldn't do things like that with find/replace/regex but try using DOM for HTML parsing. However if you're set on just manipulating the string, then you can easily achieve this with a simple regex:

$str = '<a href="/site/cdb/index.php?adinfo=272">Company Inc.</a>';

$matches = array();
preg_match('/<a href=\"[^\?]+\?adinfo=(272)\">([^<]+)/', $str, $matches);

$id = $matches[1];
$name = $matches[2];
0
php > $s = '<a href="/site/cdb/index.php?adinfo=272">Company Inc.</a>';
php > echo preg_replace('@.*adinfo=(\d+).*?>(.*?)</a>@', '$1 - $2', $s);
272 - Company Inc.
1
  • Aleks G's answer is better than mine.
    – bos
    Mar 8, 2012 at 16:33
0

If you don't want to bother with regular expressions:

Use strpos() to find the position of the substring "?adinfo=" and you will have the relative location of that url parameter's value.

Use strpos() again to find the first occurence of ">" and you will have the location of the beginning of the link text.

2
  • Think you mean the first occurrence of >.
    – Travesty3
    Mar 8, 2012 at 16:32
  • That's actually what I'm doing, the link is quite consistent, the only thing that changes is the adinfor value and the linking words and I don't know their length $str = right($str, strlen($str)-36); // '<a href="/site/cdb/index.php?adinfo=' = 36 // $str is now '272">Company Inc.</a>' $pos = strpos($str, '"'); // determine position closing quote $value = left($str, $pos); $str = right($str, strlen($str)-($pos+2)); // $str is now 'Company Inc.</a>' $pos = strpos($str, '</a>'); // determine position closing tag $link = left($str, $pos);
    – nhalink
    Mar 8, 2012 at 16:50

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.