Get index from text using PHP -
this question has answer here:
- check tag & value inside tag using php 2 answers
this question continuation of previous question:
i have text :
<organization>head of pekalongan regency</organization>, dra. hj.. siti qomariyah , ma , staff greeted <organization>rector of ipb</organization> prof. dr. ir. h. herry suhardiyanto , m.sc. , <organization>officials of ipb</organization> in guest room. with answer code question before , preg_offset_capture added :
function get_text_between_tags($string, $tagname) { $pattern = "/<$tagname\b[^>]*>(.*?)<\/$tagname>/is"; preg_match_all($pattern, $string, $matches, preg_offset_capture); if(!empty($matches[1])) return $matches[1]; return array(); } i output:
array (
[0] => array ( [0] => head of pekalongan regency [1] => 14 )
[1] => array ( [0] => rector of ipb [1] => 131 )
[2] => array ( [0] => officials of ipb [1] => 222 ) )
14, 131, 222 index of character when matching pattern. can index of word? mean output :
array (
[0] => array ( [0] => head of pekalongan regency [1] => 0 )
[1] => array ( [0] => rector of ipb [1] => 15)
[2] => array ( [0] => officials of ipb [1] => 27 ) )
is there other way preg_offset_capture or need more code? have no idea. help. :)
this work, need little bit of finishing up:
<?php $raw = '<organization>head of pekalongan regency</organization>, dra. hj.. siti qomariyah , ma , staff greeted <organization>rector of ipb</organization> prof. dr. ir. h. herry suhardiyanto , m.sc. , <organization>officials of ipb</organization> in guest room.'; $result = getexploded($raw,'<organization>','</organization>'); echo '<pre>'; print_r($result); echo '</pre>'; function getexploded($data, $tagstart, $tagend) { $tmpdata = explode($tagstart,$data); $wordcount = 0; foreach($tmpdata $k => $v) { $tmp = explode($tagend,$v); $result[$k][0] = $tmp[0]; $result[$k][1] = $wordcount; $wordcount = $wordcount + (count(explode(' ',$v)) - 1); } return $result; } ?>
and result is:
array ( [0] => array ( [0] => [1] => 0 ) [1] => array ( [0] => head of pekalongan regency [1] => 0 ) [2] => array ( [0] => rector of ipb [1] => 16 ) [3] => array ( [0] => officials of ipb [1] => 28 ) )
Comments
Post a Comment