Hello, I want to know haw can I extract some text from a file using php. Example: sometext=string-725 name="string" and nickname="string+as" Haw can I extract the value string (can be any value) from a text that has the same format, only the value of string is different, and can be found in an unknow number of times.
With a regular expression you could do this in about 4 lines of code... but... Can you be more clear about what the text file is supposed to look like EXACTLY, and what the script is supposed to take out of the file EXACTLY?
Let's say is a html file and I want to extract a value from a url inside that html file. We will have "http://www.mysite.com/index.php?articleid=3200" I want to take the value of articleid from that html file, every time an url with that structure is found in that html file.
u can use file_get_contents function, and then use regex to identify the <a> tag. I hope its useful cause its my first seed post in PHP.:d
I know haw to load the text into a variable, but I don't know what function to use and the syntax to find that url and extract articleid value.
This is off the top of my head, and its late, so you might want to check for errors But you could try something like this... <? $string = 'Some text here, maybe we got it from some source code? Here is an example link: <a href="http://site.com?id=0">test</a>!'; preg_match("/<a href=\".*\">.*<\/a>/", $string, $matches); print 'Our String: ' . $string; print '<br /><br />'; print 'Match: ' . htmlspecialchars($matches[0]); ?> PHP: You're going to need to fix the regex (the first option in preg_match between the quotes) so that it works more efficiently. But that's the basic idea. I'll see if I can be of more help in the morning
Regex could be more efficient, try this : <? preg_match_all("/articleid=([0-9]+)/si", $your_html_data, $article_ids ); print_r($article_ids['1']); ?> PHP: