Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

php regex

Guest
Jul 12, 2009 Jul 12, 2009

hello,

I need to replace my <div> tags to <TEXT> tag, but i'm very bad at regex...

i'm tring with this

<%div%[^>]*>(.*?)</%div%>

but it does not work, what can I do, to change <div align="left">my text</div>  to <TEXT align=left">my text</TEXT>

Thanks

Pluda

TOPICS
Server side applications
1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jul 12, 2009 Jul 12, 2009

You say PHP regex in the subject line, so I have written it for PHP. It's slightly different if you're using Find and Replace in Dreamweaver.

$text = <<<EOL
<div id="div1">Content for  id "div1" Goes Here</div>
<div id="div2">Content for  id "div2" Goes Here</div>
EOL;
$find = '{<div([\w\W]*?)</div>}';
$new = preg_replace($find, '<TEXT\1</TEXT>', $text);
echo $new;

Translate
LEGEND ,
Jul 12, 2009 Jul 12, 2009

You say PHP regex in the subject line, so I have written it for PHP. It's slightly different if you're using Find and Replace in Dreamweaver.

$text = <<<EOL
<div id="div1">Content for  id "div1" Goes Here</div>
<div id="div2">Content for  id "div2" Goes Here</div>
EOL;
$find = '{<div([\w\W]*?)</div>}';
$new = preg_replace($find, '<TEXT\1</TEXT>', $text);
echo $new;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 13, 2009 Jul 13, 2009

Hello,

thank you for reply, it solved my problem :-), I do not realy untherstand this regex things, but thei're a big, big help sometimes.

Thanks

Pluda

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 30, 2009 Jul 30, 2009

Hello,

sorry for re asking,

and if I need to delete the tag, but keeping the content?

example

<FONT FACE='arial' SIZE='12' COLOR='#000000' LETTERSPACING='0' KERNING='0'>content goes here</FONT>

will be plain text like "content goes here"

note, this html is flash html, but it doesn't mater, isn't it?

Thanks!

Pluda

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 31, 2009 Jul 31, 2009

still can't make this work, anyone?

thanks

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 31, 2009 Jul 31, 2009

Are you doing this in PHP or in Dreamweaver? If you're doing it in Dreamweaver, you don't need a regex.

In the Find and Replace dialog box, select Search Specific Tag, and select font from the drop-down menu. Click the minus button to remove any conditions, and set the Action drop-down to Strip Tag.

If it's a regex you need, try this:

$pattern = '/<FONT[^>]*>([\w\W]+)<\/FONT>/i';

$stripped = preg_replace($pattern, '$1', $original);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 02, 2009 Aug 02, 2009

hello, again, thanks for help

yes, I'm doing this in dreamweaver, but with php code, so ist's a regex I need.

yours, as usual works, but I don't untherstand it, please tell me, where in this patern is the content of the div, or text, or whatever?

example, I found this in internet

#</?FONT[^>]*>#is

you provided me this one

'/<FONT[^>]*>([\w\W]+)<\/FONT>/i'

yours uses /i, the other one uses #is

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 02, 2009 Aug 02, 2009

Except when used in the Dreamweaver Find and Replace feature, a regex needs to be surrounded by delimiters. Normally a pair of forward slashes are used, but you can use just about any punctuation character that doesn't have a special meaning in a regular expression. So, my regex delimiters are /regex/. The one you found uses #regex#. Using # means you don't need to escape forward slashes inside the regex. Notice that I preceded the forward slash  in </FONT> with a backslash. The other regex just uses the forward slash.

The "i" at the end of my regex makes it case-insensitive. The "is" at the end of the other one makes it both case-insensitive, and tells a period to match newlines.

The difference between the two regexes is that mine looks for both the opening and closing font tags, and uses parentheses to capture the content like this:

([\w\W]+)

This matches anything, including newline characters.

The other regex simply deletes the font tags. They both work, but in different ways. Looking at the one you found, I think it's probably more efficient than the one I created. Thanks for sharing.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 05, 2009 Aug 05, 2009
LATEST

and thank you or teaching 🙂

it's working, but still with one error

using

$tags_flash = array('#</?P[^>]*>#is');

$tags_html = array('<p class="login_texto">$1</%p%>');

$row["texto"] = preg_replace($tags_flash, $tags_html, $row["texto"]);

I got

<p class="login_texto"></%p%>Content here<p class="login_texto"></%p%>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines