84959

Converting uppercase html tags to lowercase

I have the following to replace uppercase HTML tags with lowercase ones.

$output = preg_replace("%<(/?[A-Z].*?)>%s",strtolower('$1'),$output);

The matching seems to be working well (in my RegEx testing site), but the replacement isn't.

<EM>TEST</EM> becomes EMTEST/EM

Hoping someone can point me in the right direction on this.

Answer1:

You are calling strtolower on "$1" and then using the result (which is just $1 again) to replace to.

Instead, use preg_replace_callback and have the callback be: function($m) {return strtolower($m[0]);}

Answer2:

$output = preg_replace("%<(/?[A-Z].*?)>%se", "'<' . strtolower('\\1') . '>'",$output);

<strong>Edit</strong>: forgot to mention that you should NOT use preg for HTML stuff :) DOMDocument is a far better choice.

Recommend

  • PHP If Statement conditions not working
  • Undefined Offet Error in cURL Code
  • Replacing array key spaces with underscores in multidimensional array
  • image captcha in php
  • How do I chomp a string if I have Perl 4?
  • javascript Confirm replacement with return true/false
  • Insert space after period using sed
  • FOSRestBundle: The controller must return a response (Array()) given
  • Symfony2 and MVC - Is extend controller a good practice?
  • Regex for Specific Tag
  • Efficient User-Agent Regex to find Safari in Python
  • Does CSS support inheritance [duplicate]
  • Use sed with regex and (
  • CSS - Cannot get one spanned style to override another inherited style and align left
  • During installation of Django, why do I keep getting ImportError: No module named django?
  • pillow imaging ImportError
  • Why use database factory in asp.net mvc?
  • Not able to aggregate on nested fields in elasticsearch
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • Swift: Switch statement fallthrough behavior
  • Extracting HTML between tags
  • ImageMagick, replace semi-transparent white with opaque white
  • MongoDB in PHP using aggregate to group by _id is null not working
  • All Classes Conforming to Protocol Inherit Default Implementation
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • Regex thinks I'm nesting, but I'm not
  • sending/ receiving email in Java
  • JSON with duplicate key names losing information when parsed
  • Websockets service method fails during R startup
  • Jquery - Jquery Wysiwyg return html as a string
  • Matrix multiplication with MKL
  • How to get icons for entities from eclipse?
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • Hits per day in Google Big Query
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • File not found error Google Drive API
  • JaxB to read class hierarchy
  • Converting MP3 duration time