pickachu Posted January 6, 2008 Share Posted January 6, 2008 Hi everyone, i have tried searching google but i never get my desired result. Here is my problem, I making a program that needs to read a specific content. Example, inside a html file.. <html> <head></head> <body> <p>PHP freaks!!! Awesome site!!! blah blah.. Here is your first number: 1 and your second number: 2</p> </body> </html> I need to put the value of first number (which is 1) inside a variable, and the second number to aother variable. How do I this? I dont have any idea where to start with this.. Quote Link to comment Share on other sites More sharing options...
dsaba Posted January 6, 2008 Share Posted January 6, 2008 $string = 'hello 1 bye 2'; $pat = '~hello (.) bye (.)~'; preg_match_all($pat, $string, $o); $num1 = $o[1][0]; $num2 = $o[2][0]; To extract certain data out of any other data, this is called "parsing" through the data. Regex is a handy & powerful language that can easily parse through data with symbols that make the engine do different things. There are plenty of other ways to parse through data, like creating code algorithms in php. <a href="http://regexadvice.com/blogs/mash/default.aspx">Asking Regex Questions</a> Quote Link to comment Share on other sites More sharing options...
pickachu Posted January 6, 2008 Author Share Posted January 6, 2008 Thanks for the reply, im new to regex.. with your code example, why do you have this two dimensional array? $num1 = $o[1][0]; $num2 = $o[2][0]; Quote Link to comment Share on other sites More sharing options...
Lumio Posted January 10, 2008 Share Posted January 10, 2008 Maybe you are interested in reading the manual of preg_match_all here greetings 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.