Регулярное выражение PHP для удаления тегов в документе HTML

Скажем, у меня есть следующий текст

..(content)............. <A HREF="http://foo.com/content" >blah blah blah </A> ...(continue content)... 

Я хочу удалить ссылку, и я хочу удалить тег (сохраняя текст между ними). Как это сделать с помощью регулярного выражения (поскольку все URL-адреса будут разными)

Большое спасибо

Избегайте регулярных выражений, когда это возможно, особенно при обработке xml . В этом случае вы можете использовать strip_tags() или simplexml , в зависимости от вашей строки.

Это приведет к удалению всех тегов:

 preg_replace("/<.*?>/", "", $string); 

Это приведет к удалению только тегов <a> :

 preg_replace("/<\\/?a(\\s+.*?>|>)/", "", $string); 
 <?php //example to extract the innerText from all anchors in a string include('simple_html_dom.php'); $html = str_get_html('<A HREF="http://foo.com/content" >blah blah blah </A><A HREF="http://foo.com/content" >blah blah blah </A>'); //print the text of each anchor foreach($html->find('a') as $e) { echo $e->innerText; } ?> в <?php //example to extract the innerText from all anchors in a string include('simple_html_dom.php'); $html = str_get_html('<A HREF="http://foo.com/content" >blah blah blah </A><A HREF="http://foo.com/content" >blah blah blah </A>'); //print the text of each anchor foreach($html->find('a') as $e) { echo $e->innerText; } ?> 

См. PHP Simple DOM Parser .

Не красиво, но делает работу:

 $data = str_replace('</a>', '', $data); $data = preg_replace('/<a[^>]+href[^>]+>/', '', $data); 

strip_tags() также может использоваться.

См. Примеры здесь .

Я использую это, чтобы заменить якоря текстовой строкой …

 function replaceAnchorsWithText($data) { $regex = '/(<a\s*'; // Start of anchor tag $regex .= '(.*?)\s*'; // Any attributes or spaces that may or may not exist $regex .= 'href=[\'"]+?\s*(?P<link>\S+)\s*[\'"]+?'; // Grab the link $regex .= '\s*(.*?)\s*>\s*'; // Any attributes or spaces that may or may not exist before closing tag $regex .= '(?P<name>\S+)'; // Grab the name $regex .= '\s*<\/a>)/i'; // Any number of spaces between the closing anchor tag (case insensitive) if (is_array($data)) { // This is what will replace the link (modify to you liking) $data = "{$data['name']}({$data['link']})"; } return preg_replace_callback($regex, array('self', 'replaceAnchorsWithText'), $data); } 
 $pattern = '/href="([^"]*)"/'; 

использовать str_replace