SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
Thread: String replacement question
-
Oct 15, 2002, 10:47 #1
String replacement question
How can i create string replacement like this:
<a href="link"><img src="picture" align="center(left,right)"></a>
for example like this:
[L="link"][img align="align"]imagename.jpg[/img][/L]
-
Oct 15, 2002, 10:55 #2
- Join Date
- Oct 2002
- Location
- Cincinnati, Ohio, USA
- Posts
- 390
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Re: String replacement question
Originally posted by bola
How can i create string replacement like this:
<a href="link"><img src="picture" align="center(left,right)"></a>
for example like this:
[L="link"][img align="align"]imagename.jpg[/img][/L]
-
Oct 15, 2002, 10:56 #3
- Join Date
- Mar 2001
- Location
- Mexico
- Posts
- 1,287
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Well, I'd use a regular expression. Something like this
Code:$st = '<a href="link"><img src="picture" align="center(left,right)"></a>'; // first we process <IMG> tag ... $st = preg_replace('/<img src="([^"]+)" align="([^"]+)">/i','[img align="\2"]\1[/img]',$st) // .. then <A> $st = preg_replace('/<a href="([^"]+)">([^<]+)<\/a>/i','[L="\1"]\2[/L]',$st) // note that the order is irrelevant
-
Oct 15, 2002, 10:57 #4
- Join Date
- Sep 2002
- Location
- Dallas, Tx.
- Posts
- 795
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP Code:<?php
$string='[L="link"][img align="align"]imagename.jpg[/img][/L]';
preg_match("/\[L=\"([a-zA-Z0-9\.\-]*)\"\]\[img align=\"(align|center|left|right)\"\]([a-zA-Z0-9\.\-]*)\[\/img\]/",$string,$matches);
print_r($matches);
echo "<a href=\"$matches[1]\"><img src=\"$matches[3]\" align=\"$matches[2]\"></a>";
?>PHP News, Views and Community: http://www.phpdeveloper.org
-
Oct 15, 2002, 11:04 #5
- Join Date
- Mar 2001
- Location
- Mexico
- Posts
- 1,287
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally posted by enygmadae
you know, you'll never learn regexps if you don't try first....If you have any questions you can post it here.
Paul
-
Oct 15, 2002, 11:12 #6
Ok. I'll have some fun now , and see if I can make it ...
Bookmarks