Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

preg_replace please 1

Status
Not open for further replies.

elck

Programmer
Apr 19, 2004
176
NL
Code:
$a="text<amg ||something||something_else||a_number|| >more text";

Hi, I am looking voor a piece of preg_replace code that will give me:

$b="text<img src=something alt=something_else id=a_number>more text";

Thanks in advance!

 
not enough meaningful data.

what do you want to replace?
in what ?
what with ?
to get what ?

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
I want to change all occurences of <amg || xx || yy || zz || > in a text into <img src=xx alt=yy id=zz >

In other words, within the <amg> tag I have 3 variables, separated by ||
If || is an akward separator then I could put something else, like comma's

 
I use this one for instance:
$b = preg_replace ('/\[#([^\]]*):([^\]]*)\]/', '<a href=$1>$2</a>', $b);

it replaces [#something:something_else] with
<a href=something>something_else</a>

Now I am looking for something simular, but with three arguments.

 
why use preg_replace for this?

try the code below
Code:
<?
$a="text<img ||something||something_else||a_number|| >more text";
$array = explode("||",$a);


$c = 	$array[0] . 
		"src=\"".$array[1]."\"
		 alt=\"".$array[2]."\" 
		 id=\"".$array[3]."\" " .$array[4];
echo $c; //nb you need to look at the source code to see this working properly (or use htmlspecialchars)
?>
 
Because $a is a long text with numerous occurences of "<amg..."
A version without regular expressions I can manage, it is the regular expression that I am looking for.

 
i'd still recommend against a complete use of regex. perhaps you could use regex to capture the amg tags into an array, use a non-regex function to parse them into a proper form and fix amg=>img. i'll bet this is faster by far (certainly easier to work out)
 
Code:
while ($i=strpos($pagtekst,"<amg")) {
	$j=strpos($pagtekst,">",$i);
	$a=substr($pagtekst,$i,$j-$i);
	$b=explode("||",$a);
	$c="<img src=\"" . $b[1] . "\" onclick=\"location.href='" . $b[2] . "'\" id=\"P" . $b[3] . "\" style='cursor:pointer;'>";
	if ($b[3]==0)
	$pagtekst=$c . substr($pagtekst,0,$i)  . substr($pagtekst,$j+1);
	else
	$pagtekst=substr($pagtekst,0,$i) . $c . substr($pagtekst,$j+1);
}

As you can see the real code is a bit more complex.
In the example above I move the first image to the front of the file while leaving the rest where they are.

Later on I wish to perform more complex task depending on the number and place of the images

I don't think calculating speed would slow down a website with large pictures.

I think regex makes things far more concise and easier to understand (not the regex, but the complete code)
but maybe that is a matter of taste.

Thanks for looking at it though


 
looks ok to me! i might have used regex to grab the <amg tags though. I'm not good on regex but i don't think it can be used the way you wanted (other than, of course, iterative preg_replace statements searching for "||").

 
There is a relative simple solution making all iterations unnecessary. Even though many people don't like regular expressions, I find them extremely useful and it becomes a skill to craft complex structures that do complicated tasks.
The pipe characters are not an ideal delimiter, especially in the PCRE context. This is because the pip character has meta meaning and signifies the start of an alternate branch. You'll see a lot of escaping in the expression to turn the pipes into literal pipe characters:
Code:
$text = "<amg || xx || yy || zz || >
Other stuff here
 in here also <amg || 12341234 || 213432144sldflll is here || wqeewee || >";
// in a text into <img src=xx alt=yy id=zz >
$pattern = "/<amg \|\| (.*?) \|\| (.*?) \|\| (.*?) \|\| >/i";
$ntext= preg_replace($pattern,"<img src='\\1' alt='\\2' id='\\3'>",$text);
echo(htmlentities($ntext));
echo($ntext);
A one-line preg_replace will take care of all instances of the amg tags in a string. I added the "i" pattern modifier just to make sure AMG tags would be included also. If that's not needed, just remove it.
 
Righto!
Great, that was I was looking for.

Do you think that regex is so slow that a webpage full of images becomes slower?
Iterations take time too!
 
Depends a lot on the hardware, but I think it should not make a significant difference. You could try to time it.

Hardware example:
Someone here just recently wrote a really complicated Web app (a calendaring tools) that does a lot of integer operations, date operations, loops. On our SUN systems (which are quad processors with 8GB RAM) the thing runs really really slow, because SUN/Solaris is slow on these operations. We changed the backend to a Linux box with a single Intel processor and the performace improves dramatically.
It depends on the hardware.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top