torb Posted March 13, 2010 Share Posted March 13, 2010 Hi there! I have been browsing for quiiite a few hours both here and in the PHP manual looking for a simple way to find some characters in strings. I've tried regex and all sorts of preg_matches and whatnot, but my skills simply aren't there. Anyways, here's my example. Let's say a forum post string reads like this... Sed fermentum velit vel orci posuere nec ultrices lacus congue. Phasellus non justo non nibh elementum consectetur. Praesent ut magna orci. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. [special]42 Quisque id mi turpis, in fringilla tortor. Vestibulum rutrum adipiscing lacus. Duis auctor cursus consectetur. Quisque id arcu at eros laoreet vulputate. Nam tincidunt nunc id sem sagittis pharetra. Aenean eu sapien ut quam dapibus ultrices. ...then I want the result value 42 in a new variable. So basically, I want the two characters AFTER [special] as a new variable... Any help is greatly appreciated! -torb PS: I would be very willing to change it to [special]42[/special] to make it look more like bbCode. Quote Link to comment Share on other sites More sharing options...
schilly Posted March 13, 2010 Share Posted March 13, 2010 this would be easy with preg_match but my patterns suck so i would just do: $first_occurance = strpos($post,"[special]"); $special_code = substr($post,$first_occurance+9,2); Quote Link to comment Share on other sites More sharing options...
torb Posted March 13, 2010 Author Share Posted March 13, 2010 Thank you, that works. It seems a bit dirty with the hard-coded "+9" and all, but it will do until I come across something a bit more sanitary Quote Link to comment Share on other sites More sharing options...
schilly Posted March 13, 2010 Share Posted March 13, 2010 yea anyone good with pattern matching could make a good pattern for it but the pattern matching syntax never set in for me. 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.