Zeradin Posted October 6, 2008 Share Posted October 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/127229-regular-expression-help/ Share on other sites More sharing options...
Orio Posted October 6, 2008 Share Posted October 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/127229-regular-expression-help/#findComment-658060 Share on other sites More sharing options...
ghostdog74 Posted October 6, 2008 Share Posted October 6, 2008 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); Quote Link to comment https://forums.phpfreaks.com/topic/127229-regular-expression-help/#findComment-658063 Share on other sites More sharing options...
Zeradin Posted October 6, 2008 Author Share Posted October 6, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/127229-regular-expression-help/#findComment-658067 Share on other sites More sharing options...
Zeradin Posted October 6, 2008 Author Share Posted October 6, 2008 crap i forgot i wanted to add like hypimage(image, alignment, caption) where caption would be a string like this is a picture that i uploaded. how can i include that to make it $3 and will it need to be escaped for certain characters? Quote Link to comment https://forums.phpfreaks.com/topic/127229-regular-expression-help/#findComment-658074 Share on other sites More sharing options...
nrg_alpha Posted October 6, 2008 Share Posted October 6, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/127229-regular-expression-help/#findComment-658118 Share on other sites More sharing options...
Zeradin Posted October 6, 2008 Author Share Posted October 6, 2008 Nice. thank you so much. It worked perfectly! Quote Link to comment https://forums.phpfreaks.com/topic/127229-regular-expression-help/#findComment-658398 Share on other sites More sharing options...
Zeradin Posted October 6, 2008 Author Share Posted October 6, 2008 why can't i mark this as solved? Quote Link to comment https://forums.phpfreaks.com/topic/127229-regular-expression-help/#findComment-658399 Share on other sites More sharing options...
DarkWater Posted October 6, 2008 Share Posted October 6, 2008 The forums were upgraded today and the Solved button was accidentally removed, but it should be back up soon. Quote Link to comment https://forums.phpfreaks.com/topic/127229-regular-expression-help/#findComment-658404 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.