[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Regular expression

Posted on 2006-04-07
3
Medium Priority
?
259 Views
Last Modified: 2006-11-18
Hi.
I need to replace, in all <img ... /> tags of HTML code with the following data:

' align="right" '      with     ' align="right" style="border:1px" '
' align="left" '      with     ' align="right" style="border:12px" '

Any help or clue to build a Regular expression?

Tank you for reading and for help.
0
Comment
Question by:valverde2006
  • 3
3 Comments
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 2000 total points
ID: 16400482
<?php
$s_old_html = <<< END_HTML
<img src="">
<img align="left" src="">
<img src="" align="left">
<img align="right" src="">
<img src="" align="right">

END_HTML;

// Make sure you do the right=>right first as left=>right=>right otherwise
$as_search = array
      (
      '`(<img.*?)align="right"(.*?>)`sim',
      '`(<img.*?)align="left"(.*?>)`sim',
      );
      
$as_replace = array
      (
      '\1align="right" style="border:12px"\2',
      '\1align="right" style="border:1px"\2',
      );

$s_new_html = preg_replace($as_search, $as_replace, $s_old_html);

echo "Old HTML\n$s_old_html\n\nNew HTML\n$s_new_html\n\n";
?>
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 16400587
The above code outputs ...

Old HTML
<img src="">
<img align="left" src="">
<img src="" align="left">
<img align="right" src="">
<img src="" align="right">


New HTML
<img src="">
<img align="right" style="border:1px" src="">
<img src="" align="right" style="border:1px">
<img align="right" style="border:12px" src="">
<img src="" align="right" style="border:12px">
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 16406807
Thanks for the points.

If you need any more help, always come back.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question