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

Link to comment
Share on other sites

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);

Link to comment
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.

 

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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