tags:

views:

141

answers:

5

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.

+3  A: 

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
more complicated version of `strstr($link_item, 'http://');` isn't it?
Gordon
thanks you @Felix
Happy
@Gordon, don't waste your time :)
Happy
@Glister I know that @Felix appreciates constructive criticism, so it's not wasted on him.
Gordon
@Gordon: I was not aware of this function. You are right. And yes it is not wasted, I am always happy to learn :)
Felix Kling
@Gordon: I added your comment to my answer. If you want to provide your own answer with this solution, I will delete it again.
Felix Kling
A: 

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

Ignacio Vazquez-Abrams
Would you really load an external module just for this trivial substitution?
kemp
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
+1  A: 

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
like mine.hope it works, thanks Sarfraz.
Happy
@Glister: You are welcome :)
Sarfraz
+3  A: 
$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
What about multiple, nested or none `<span>` s ?. Of course we can only assume here as the OP doesn't give enough information. So "assuming" that there will never be a `<span>` after the URL, maybe this is more robust: `echo $pieces[count($pieces) -1 ]` (just get the last element of the array).
Felix Kling
$string doesn't everytime have "<span>". Thanks for try.
Happy
@Glister: How can we know?
Felix Kling
@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.
kemp
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
@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
@calumbrodie: If you consider the small change in my comment, it even if no `<span>` is present.
Felix Kling
@felix I've edited my answer. thanks
calumbrodie
A: 

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
why minus? I think this is the best solution
Happy
Indeed, downvote compensated
kemp
-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
It would still be nice if you could provide us more information about the structure of `$link_item`.
Felix Kling
@Gordon: how is it inefficient?
kemp
@Gordon, I had no idea hot to fix when this post was published.
Happy
You said in my answer that "$string doesn't everytime have "<span>". Thanks for try"In that case your code would not work!
calumbrodie
@calumbrodie, don't know how, but my solution works like expected
Happy
@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
@Glister Sorry, but that's not good enough, I've downvoted your question.
calumbrodie