So I have this string that looks like this:
I/O complained/O to/O Microsoft/ORGANIZATION about/O Bill/PERSON Gates/PERSON ./O They/O told/O me/O to/O see/O the/O mayor/O of/O New/LOCATION York/LOCATION ./O
and I want to output it like this:
I complained to <span class="ORAGANIZATION"> Microsoft</span> about <span class="PERSON">Bill</span>
Im done with the /O thing i did it with just preg_replace what my problem is the Microsoft/ORGANIZATION i need the REGEX for this. I tried finding the word first using this:
$text_vars = preg_match("/PERSON|ORGANIZATION/", $string, $matches);
$temp = array_shift( $matches );
preg_replace
solution:
$s = 'I/O complained/O to/O Microsoft/ORGANIZATION about/O Bill/PERSON Gates/PERSON ./O They/O told/O me/O to/O see/O the/O mayor/O of/O New/LOCATION York/LOCATION ./O';
$result = preg_replace('~(\S+)/(ORGANIZATION|PERSON)~', '<span class="$2">$1</span>',
preg_replace('~/O(\s+|$)~', ' ', $s));
print_r($result);
The output:
I complained to <span class="ORGANIZATION">Microsoft</span> about <span class="PERSON">Bill</span> <span class="PERSON">Gates</span> . They told me to see the mayor of New/LOCATION York/LOCATION .