Jump to content

regular expression help


Zeradin

Recommended Posts

I am really new to regular expressions, but I've been going over it trying to get it, but I can't seem to understand it properly

I am trying to match this:

 

hypimage(imagename.imageext,alignment)

 

and i want to change it later to something like

<image src="images/imagename.imageext" align="alignment">

 

but I can't get it find the string properly. I have this and I don't understand what is wrong with it:

/hypimage\(<[a-zA-Z0-9]+>\.<[a-zA-Z0-9]+>,<[a-zA-Z0-9]+>\)/i

 

what am i doing wrong? and how to I call the variable words in the new expression? thanks.

Link to comment
https://forums.phpfreaks.com/topic/127229-regular-expression-help/
Share on other sites

You can't use "<" and ">" instead of closing and opening brackets. This should do:

 

<?php

$data = "some text.here hypimage(pic.jpg, left) more-text";

$pattern = "/hypimage\(([a-z0-9]+\.[a-z0-9]+),\s*([a-zA-Z0-9]+)\)/i";
$replacement = '<image src="images/$1" align="$2">';
$data = preg_replace($pattern, $replacement, $data);

echo htmlentities($data); //Remove htmlentities when in use... It was added only for checking the output

//output:   some text.here <image src="images/pic.jpg" align="left"> more-text

?>

 

Check out preg_replace() if you don't understand the $1, $2 references.

 

Orio.

using the normal string operations if you are not familiar with regexp, yet.

$string = "hypimage(imagename.imageext,alignment)";
$hypstart = strpos($string,"hypimage") ;
$start= strpos($string,"hypimage") + strlen("hypimage");
$end = strrpos($string,")"); 
$words_in_brackets = substr($string,$start+1,$end-$start-1);

You can't use "<" and ">" instead of closing and opening brackets. This should do:

 

<?php

$data = "some text.here hypimage(pic.jpg, left) more-text";

$pattern = "/hypimage\(([a-z0-9]+\.[a-z0-9]+),\s*([a-zA-Z0-9]+)\)/i";
$replacement = '<image src="images/$1" align="$2">';
$data = preg_replace($pattern, $replacement, $data);

echo htmlentities($data); //Remove htmlentities when in use... It was added only for checking the output

//output:   some text.here <image src="images/pic.jpg" align="left"> more-text

?>

 

Check out preg_replace() if you don't understand the $1, $2 references.

 

Orio.

 

thanks! i'll give that a shot. can you explain why there is a (( and a \s* ? i don't really know those

using the normal string operations if you are not familiar with regexp, yet.

$string = "hypimage(imagename.imageext,alignment)";
$hypstart = strpos($string,"hypimage") ;
$start= strpos($string,"hypimage") + strlen("hypimage");
$end = strrpos($string,")"); 
$words_in_brackets = substr($string,$start+1,$end-$start-1);

 

nice! thanks. i didn't even think of this

can you explain why there is a (( and a \s* ? i don't really know those

Parenthesis are captures that assign numerical variables to what is found within them.. therefore anything matched within the first set of parenthesis is stored as $1, the next as $2, etc.. the \s is a short hand character class that encapsulates all forms of spaces (a literal space, a tab, return carriage, new line, etc..) The star after it is a quantifyer meaning zero or more times.

 

You can read up on PCRE here and here.

 

@Orio

 

Your preg pattern can also be as such:

$pattern = "#hypimage\(([-\w]+\.(jpe?g|gif|png|bmp)),\s*(\w+)\)#i";

 

Cheers,

 

NRG

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.