how do i parse out the "<img src... />" strings ? now i'm excited. all i really know of php are the random and array functions
For that you can use Regex.
[PHP]if (preg_match("/<img src=\"([^\"]+)\" .*? alt=\"([^\"]+)\" .*? \/>/", $item['description'], $matches)) {
$img_html = $matches[0];
$img_src = $matches[1];
$img_title = $matches[2];
// ouput image
}[/PHP]
I thought I'd take the time to explain the regex:
I used the function preg_match, which takes a regular expression (string pattern) and matches the pattern in the $item['description'] string.
The matches are set as an array in the third parameter, in this case $matches.
Heres the regex expression:
"/<img src=\"([^\"]+)\" .*? alt=\"([^\"]+)\" .*? \/>/"
It is quite simple:
a regext expression starts with a delimiter, in this case /. When the regex engine finds another instance of / it will end the pattern and assume every character after the second delimiter are arguments passed with the regex expression.
$matches[0] will contain a match of the whole pattern (if a match is made).
$matches[1] contains a matche of the first set of parantheses () and so on.
square brackets allow you to match any character inside the brackets.
^ right after the first square bracket tells the regex engine to match any char other than whats in the square brackets.
. is any single character.
* is any number of the character right before it.
+ is one or more of the chracter right before it.
\ is used to escape characters, meaning the character right after the \ is treated as a literal value and does not have any special meaning. eg: \* means match the asterix itself.
? means either or? or 0 or 1 of the character right before it. If it is placed right after a quantifier eg: .*? then it tells the regex engine to match "lazily" - match the smallest possible match, as opposed to matching "greedily" where the largest match is made. eg: In "<tag> cotent </tag>", the regex "<.*>" will match the whole string, however, "<.*?>" will only match "<tag>".
So the regex expression "/<img src=\"([^\"]+)\" .*? alt=\"([^\"]+)\" .*? \/>/" reads:
<img src=\" : Matche a string starting with <img src="
([^\"]+) : create a new match in $matches[1] and match all the chracters that aren't a double quote, ".
.*? continue matching any character (lazily) untill you come to: alt=
alt=\" : match alt="
([^\"]+) : create a new match in $matches[2] and match all the chracters that aren't a "
.*? \/> : match any chracter (lazily) till you reach />
Now you get a resulting array: $matches with the tree matches inside. $matches[0] being the whole string. $matches[1] being the first ([^\"]+) and $matches[2] being the second ([^\"]+)