0

I want this:

<h2>My Headline</h2>

to this:

<span class="h2">My Headline</span>

I tried this, but this is just a way to remove the h-tag with the content :/

$introtext = preg_replace('/<h2[^>]*>([\s\S]*?)<\/h2[^>]*>/', '', $introtext);

Maybe there is a solution to "convert" all h-tags into span-tags with the right class?

Thanks for support.

Another idea to change the tags, was this:

$introOld = array('/<h2>/','/</h2>/');
$introNew = array('<span class="h2">','</span>');
$introtext = preg_replace($introOld, $introNew, $introtext);

Maybe this will be help to show, what I mean/want.

I have on content ($introtext) and to views (teaser [categoryView] and detail [articleView]). For the categoryView I want change all h-tags into span-tags, because of the category description. If the articleView is shown, h-tags are okay.

deathride
  • 28
  • 3
  • is this a homework assignment or something? – PlantTheIdea Jan 05 '15 at 13:19
  • Bear in mind that your replacement is invalid—you'll need ``, not ``. Also, *why* do you want to do this? There may be a better solution (changing the styling of your `h2` elements with CSS, for example.) Also: http://stackoverflow.com/a/1732454/300836 – Matt Gibson Jan 05 '15 at 13:23
  • Oh. Thanks. `My Headline` is wrong. That meant `My Headline` – deathride Jan 05 '15 at 13:50
  • @Matt Gibson: This solution is okay, if i want just style my heading. But i need another tag to remove the "importance" of the headings [SEO]. – deathride Jan 05 '15 at 13:58
  • @deathride: What if you encounter a tag containing whitespace? for example: ` h2>`. Common? No. Possible: yes. – Elias Van Ootegem Jan 05 '15 at 14:32
  • @EliasVanOotegem: I would exclude this. The tags will be generated by user (backend - wysiwyg including auto correction). – deathride Jan 05 '15 at 14:53
  • @deathride: That's just an example of things you can't reliably foresee when parsing HTML with regular expressions, which is exactly why I suggested using a parser, like `DOMDocument` is: it's a lot easier to read (and maintain) than a regular expression is, and it's equipped to deal with things like nested tags, or strangely formed markup – Elias Van Ootegem Jan 05 '15 at 15:34
  • Depending on how your users edit content in Joomla, you may be able to just replace the style that they're choosing in the editor that produces the `h2` with one that produces a `span` instead... – Matt Gibson Jan 05 '15 at 16:34

1 Answers1

1

For the record: Markup (HTML/XML) and Regex don't mix well. Regular expressions just can't cope with the complexities of markup languages, so any attempt at processing markup using regex will end in tears eventually. Luckily, PHP has a DOM parser you can use, which makes it relatively easy to do what you want to do:

$dom = new DOMDocument();
$dom->loadHTML($yourMarkup);
$headers = $dom->getElementsByTagName('h2');
foreach ($headers as $header)
{
    $span = $dom->createElement('span', $header->nodeValue);//create span with h2's value
    $span->setAttribute('class', 'h2');
    $header->parentNode->replaceChild($span, $header);//replace element
}

Granted, this code is a tad verbose, but then DOM api's tend to be verbose. Anyway, the DOMDocument, DOMElement and DOMNode classes are well documented. Read through the docs, take notes and get cracking.

Community
  • 1
  • 1
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149