Jump to content

Recommended Posts

hi friends
i want to know that how can i replace the entire content in <..............> tags.

for example
i have a code
<STRONG><I><B><FONT COLOR="red">Example Text</FONT></B></I></STRONG>

and i want to convert the html tags to small caps without converting the text i.e Example Text.

<STRONG> should be <strong>
<I> should be <i>
<FONT COLOR="red"> should be <font color="red">

but keeping the text i.e Example Text intact.
due to which i can't use strtolower.

please tell me how to convert the html tags into smallcaps.
what regular expression to use so that i get

<strong><i><b><font color="red">Example Text</b></i></font></strong>

thanks
please help
Link to comment
https://forums.phpfreaks.com/topic/30541-how-to-convert-to-small-caps/
Share on other sites

This almost works...

[code=php:0]<?php
// Test html code
$html = <<<HTML
<HTML>
<HEAD>
<TiTlE>This is My Title</TITlE>
<body BG="Red">
This is Some Text
</body>
</HtML>
HTML;

// Regular expression
echo preg_replace("/(<.*?>)/e", "''.strtolower('$1').''", $html);
?>[/code]

The problem is that it escapes all the single and double quotes in the output.  Here's what it produces...

[code]<html>
<head>
<title>This is My Title</title>
<body bg=\"red\">
This is Some Text
</body>
</html>[/code]

But it's a start.

Regards
Huggie
Huggie, you've got to be careful with your solution, because it will also change any attribute values to lowercase, and when you're referencing a URL, javascript function or a class/id for CSS, case sensitivity is in play. It's a great start, but you'll want to parse a bit further and make sure that nothing within single or double quotes is changed as well.
[quote author=taith link=topic=118504.msg486148#msg486148 date=1166281972]
next question... html is non case specific, and if you just set your css/js to all lowercase too, who cares? or make the css/js seperate variables...
[/quote]

Because all lowercase in coding is very seldom best practice. When defining functions or other attributes of coding languages, there are often cases where camel-case (ie, myFunction) is the best practice. So, you never want to throw best practices out the window to work around a problem. You're much better off to find a solution that will work in conjunction with the best practices you have put in place. As for your comment about making the CSS and Javascript separate variables, I'm not sure I follow what you're after.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.