3

I have the following html content:

<p>My name is <a href="way2project">way2project</a></p>

Now I want this text as <p>My name is way2project</p>

Is there any way to do this? Please help me thanks

I used preg_replace but in vain.

Thanks again

4
  • I'm not sure I understand. Do you want your hyperlink to appear like normal text? Please make your question more clear. Commented Jun 25, 2012 at 19:45
  • 1
    So $blurb = preg_replace('/<[^>]*>/', '', $blurb); doesnt work? Commented Jun 25, 2012 at 19:45
  • You may want to read into: DOMDocument. Regex is (most of the time) not suited to parse HTML. Commented Jun 25, 2012 at 19:57
  • 2
    possible duplicate of How to remove an HTML element using the DOMDocument class Commented Jun 25, 2012 at 20:05

4 Answers 4

2

You can use the strip tags function

$string = '<p>My name is <a href="way2project">way2project</a></p>';

echo strip_tags($string,'<p>');

note the second parameter is the list of allowed tags you wont to ignore.

Sign up to request clarification or add additional context in comments.

3 Comments

also, strip_tags will strip ALL HTML tags. Sounds like he only wants to remove anchor tags
@jackwanders yes it does, but you can use the second parameter to take care of that.
the second parameter lets you list allowable tags. it would work for this specific example, yes. But to reliably use this with arbitrary chunks of HTML, he'd have to list ALL html tags except for <a> in that second parameter
1

This seems strange, but not knowing the complete scope of your issue and seeing that you want to do this in PHP, you can try:

$origstring = '<p>My name is <a href="way2project">way2project</a></p>';
$newstring = str_replace('<a href="way2project">way2project</a>', 'way2project', $origstring);

echo $newstring;

Comments

1

Checkout Simple Html Dom Parser

$html = str_get_html('<html><body>Hello!<a href="http://stackoverflow.com">SO</a></body></html>');
echo $html->find('a',0)->innertext; //prints "SO"

Comments

0

strip_tags you can use this, to remove html tags.

Comments

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.