-1

I have a variable $link_item, it's used with echo and gives the strings like

<span class="name">Google</span>http://google.com

How to remove "<span class="name">Google</span>" from string?

It should give just "http://google.com".

Heard it can be done with regex(), please help.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Happy
  • 881
  • 7
  • 16
  • 32
  • 2
    What is the difference each time? What can differ, and what stays the same? – Ikke Apr 07 '10 at 08:22
  • 1
    I agree with @Ikke. Please give some more examples of the contents of `$link_item`. The current example does not provide enough information for a solution. – Gordon Apr 07 '10 at 08:25

5 Answers5

4

Without regex:

echo substr($link_item, stripos($link_item, 'http:'))

But this only works if the first part (i.e. <span class="name">Google</span>) never contains http:. If you can assure this: here you go :)

Reference: substr, stripos

Update:

As @Gordon points out in his comment, my code is doing the same as strstr() already does. I just put it here in case one does not read the comments:

echo strstr($link_item, 'http://');
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
3
$string = '<span class="name">Google</span>http://google.com';
$pieces = explode("</span>",$string);
//In case there is more than one span before the URL
echo $pieces[count($pieces) -1];
calumbrodie
  • 4,722
  • 5
  • 35
  • 63
  • 1
    What about multiple, nested or none `` s ?. Of course we can only assume here as the OP doesn't give enough information. So "assuming" that there will never be a `` after the URL, maybe this is more robust: `echo $pieces[count($pieces) -1 ]` (just get the last element of the array). – Felix Kling Apr 07 '10 at 08:38
  • $string doesn't everytime have "". Thanks for try. – Happy Apr 07 '10 at 08:40
  • @Felix: why assuming more than necessary? Just stick to the question, if it turns out to be incomplete the OP will extend it. There is no point in considering 100 possibilities when the case could be just one. – Matteo Riva Apr 07 '10 at 08:41
  • Indeed, I was working on the assumption that every $string would contain one span and one URL (in that order). If this is not the case my code won't work.. – calumbrodie Apr 07 '10 at 08:41
  • @kemp: Sometimes it doesn't hurt to think a little further ;) (only if it involves small changes to the original solution, of course there is no point in considering everything). In fact, I like this solution very much. I just added a thought. – Felix Kling Apr 07 '10 at 08:46
  • @calumbrodie: If you consider the small change in my comment, it even if no `` is present. – Felix Kling Apr 07 '10 at 08:50
1

Solved:

$contents = '<span class="name">Google</span>http://google.com';
$new_text = preg_replace('/<span[^>]*>([\s\S]*?)<\/span[^>]*>/', '', $contents);
echo $new_text;

// outputs -> http://google.com
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
0

Don't use a regex. Use a HTML parser to extract only the text you want from it.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 2
    Would you really load an external module just for this trivial substitution? – Matteo Riva Apr 07 '10 at 08:36
  • I think people are just afraid of another meltdown: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags And of course it depends on whether the HTML of `$link_item` ever changes, which I'm guessing it may one day. – Carson Myers Apr 07 '10 at 09:29
0

Made myself

$link_item_url = preg_replace('@<span[^>]*?>.*?</span>@si', '', $link_item);

This will remove any <span + something + </span> from variable $link_item.

Thanks for all.

Happy
  • 881
  • 7
  • 16
  • 32
  • why minus? I think this is the best solution – Happy Apr 07 '10 at 08:36
  • -1 for wasting people's time by asking a question the OP apparently could solve himself without help. I'd give another -1 for the misuse of Regex and another -1 for not providing the requested information to the question. But I can only downvote once. – Gordon Apr 07 '10 at 08:40
  • It would still be nice if you could provide us more information about the structure of `$link_item`. – Felix Kling Apr 07 '10 at 08:40
  • @Gordon: how is it inefficient? – Matteo Riva Apr 07 '10 at 08:43
  • @Gordon, I had no idea hot to fix when this post was published. – Happy Apr 07 '10 at 08:43
  • You said in my answer that "$string doesn't everytime have "". Thanks for try" In that case your code would not work! – calumbrodie Apr 07 '10 at 08:43
  • @calumbrodie, don't know how, but my solution works like expected – Happy Apr 07 '10 at 08:48
  • @kemp for simple string replacements, regex are almost always the slower alternative. For the example given in the question, a simple strstr would suffice. – Gordon Apr 07 '10 at 08:54
  • @Glister Sorry, but that's not good enough, I've downvoted your question. – calumbrodie Apr 07 '10 at 08:55