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

Ibu
  • 42,752
  • 13
  • 76
  • 103
way2project
  • 99
  • 3
  • 8
  • I'm not sure I understand. Do you want your hyperlink to appear like normal text? Please make your question more clear. – gkiar Jun 25 '12 at 19:45
  • 1
    So `$blurb = preg_replace('/<[^>]*>/', '', $blurb);` doesnt work? – Sammaye Jun 25 '12 at 19:45
  • You may want to read into: [DOMDocument](http://php.net/manual/en/class.domdocument.php). Regex is (most of the time) not suited to parse HTML. – PeeHaa Jun 25 '12 at 19:57
  • 2
    possible duplicate of [How to remove an HTML element using the DOMDocument class](http://stackoverflow.com/questions/1171597/how-to-remove-an-html-element-using-the-domdocument-class) – PeeHaa Jun 25 '12 at 20:05

4 Answers4

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.

Ibu
  • 42,752
  • 13
  • 76
  • 103
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"
manuskc
  • 784
  • 4
  • 14
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;
MultiDev
  • 10,389
  • 24
  • 81
  • 148
0

strip_tags you can use this, to remove html tags.

Piotr Olaszewski
  • 6,017
  • 5
  • 38
  • 65