TiloW Posted July 12, 2009 Share Posted July 12, 2009 I need to search a string for certain substrings, here are some examples of substrings i'd like to find in my string: {$content} {$title} {$moduleLeft} {$moduleRight} so i created a pattern: {$[A-Z|a-z]+} then recognized i had to escape some chars ..correct?^^ \{\$[A-Z|a-z]+\} It doesn't work properly but just finds the first occurence - im using the function ereg: ereg('{\$[A-Z|a-z]+}', $content, $positions); The array $positions should store all of thoose substrings. But $positions always consists of only 1 element. ($positions[1] is always set, no other elements are set) If i delete the first match in $content, the value of $positions[1] changes accordingly - which is why i'm not sure whether regular expression or something else is incorrect.. I'll appreciate any help Quote Link to comment Share on other sites More sharing options...
MadTechie Posted July 12, 2009 Share Posted July 12, 2009 Do you mean this ? <?php $content = '{$content} {$title} {$moduleLeft} {$moduleRight}'; preg_match_all('/\{\$[a-z]+\}/i', $content, $result); foreach($result[0] as $Item) { echo $Item; } ?> if you want just the word ie title then change the RegEx to '/\{\$([a-z]+)\}/i' and $result[0] to $result[1] Quote Link to comment Share on other sites More sharing options...
TiloW Posted July 12, 2009 Author Share Posted July 12, 2009 no.. $content contains random text and the substrings specified above. for example: $content = "<html><head><title>{$title}</title></head><body>{$content}</body></html> Quote Link to comment Share on other sites More sharing options...
MadTechie Posted July 12, 2009 Share Posted July 12, 2009 Okay.. my example input was wrong.. did you test it with your input.. and was the result what you expected ? EDIT: would this be the correct response {$title}{$content} to your input of $content = '<html><head><title>{$title}</title></head><body>{$content}</body></html>'; Quote Link to comment Share on other sites More sharing options...
TiloW Posted July 12, 2009 Author Share Posted July 12, 2009 $content = "<html><head><title>{$title}</title></head><body>{$content}</body></html>"; ereg('{\$[A-Z|a-z]+}', $content, $positions); after this the array $positions should consist of 2 elements: $positions[1] == "{$title}" $positions[2] == "{$content}" ..but for some strange reason ereg only stores the first occurrence (in this case "{$title}") in the array.. what do i have to change to store all occurrences in $positions? Quote Link to comment Share on other sites More sharing options...
MadTechie Posted July 12, 2009 Share Posted July 12, 2009 Like this (remember arrays start from 0 not 1) <?php $content = '<html><head><title>{$title}</title></head><body>{$content}</body></html>'; preg_match_all('/\{\$[a-z]+\}/i', $content, $positions); $positions =$positions[0]; echo $positions[0]; echo $positions[1]; ?> Quote Link to comment Share on other sites More sharing options...
TiloW Posted July 12, 2009 Author Share Posted July 12, 2009 even if i've got more than 2 matches only the first one is strored the first index of the resulting array is 1 ..take a look at function ereg If matches are found for parenthesized substrings of pattern and the function is called with the third argument regs , the matches will be stored in the elements of the array regs . $regs[1] will contain the substring which starts at the first left parenthesis; $regs[2] will contain the substring starting at the second, and so on. $regs[0] will contain a copy of the complete string matched. Quote Link to comment Share on other sites More sharing options...
TiloW Posted July 12, 2009 Author Share Posted July 12, 2009 oh didn't see u used another function.. well that works fine thank you! -solved- Quote Link to comment Share on other sites More sharing options...
MadTechie Posted July 12, 2009 Share Posted July 12, 2009 Okay 1. ereg is DEPRECATED, so its better to use preg, 2. The code I have supplied will give you the results and store all, 3. If you ONLY want the first one then i can change the code for that 4. topic solved button bottom left Quote Link to comment 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.