i need take img src record after query:
while ($row = mysqli_fetch_array($result, mysqli_assoc)) { $rows[] = array( 'id' => $row['id_content'], 'titolo' => $row['titolo'], 'articolo' => $row['descrizione'], 'giorno' => $row['giorno'], 'foto' => "get img src this: $row['descrizione']", 'fonte' => $row['fonte'] );}
is possible? try preg_match_all('/<img[^>]+>/i',$row['descrizione'],$res[0])
1 result!
ok solve so:
create function:
function srcimg($num) { preg_match('@src="([^"]+)"@',$num,$match); $src = array_pop($match); return $src; }
and then:
while ($row = mysqli_fetch_array($result, mysqli_assoc)) { $rows[] = array( 'id' => $row['id_content'], 'titolo' => $row['titolo'], 'articolo' => $row['descrizione'], 'giorno' => $row['giorno'], 'foto' => srcimg($row['descrizione']), 'fonte' => $row['fonte'] );} echo json_encode($rows);
i'd highly recommend @javier núñez suggest, , use html parser regex cannot cover eventualities this. if reason can't, try using regex: (untested)
src=['"]([^"]+)['"]
and can use in preg_match follows:
preg_match( '@src=['"]([^"]+)['"]@', $row['descrizione'], $match); $src = array_pop($match);
you can tinker if need hacky approach. understand urgency working on more perfect solution. ;)
you can use http://regexr.com/ test regex if want try , make more robust.
edit: didn't use preg_match_all. you'll want use instead.
Comments
Post a Comment