Err Posted May 29, 2011 Share Posted May 29, 2011 I'm trying to get the correct length of a string, but for some reason the string length is wrong. I narrowed it down to a line that has preg_replace() which replaces a term in the string with one with <span> tags around it. For example, if I searched 'cat' I would get <span>cat</span>. Anyways, after the string goes through the preg_replace the string length gets a lot bigger than it should be, even with all the <span> tags accounted for. I can't seem to figure out what is going on with it. It works fine for some strings but certain strings are completely off. I've used the exact same method by itself and I can't repeat the results. I have error reporting on but not like that's any help for this. Has anyone had any similar problems with this or can help me nail down what the issue is?The below code works fine, (small scale example of what I'm working with) but I can't figure out why it's not working on my project. $str = "Looks good, but personally liked the layout of the old site. Like the interactive capabilities so you do not have to email for new or editing of postings. For open games link, need to also add boys/girls option on the advance search. Needs info on upcoming ####o conference. Was never posted on the old site either"; $str = preg_replace("/o/i","<span>o</span>",$str); var_dump($str); Here is part of the code I'm actually working with:$row[$i] is where the string comes from$txtSrc is the text searched, could be anything for ($i = 0; $i < $cnt; $i++) { $rowIdx = ""; $des = $row[$i]; // cut string at 1k characters if (strlen($des) >= 1000) {$des = substr($des,0,1000);} if ($txtSrc) {$des = preg_replace("/$txtSrc/i","<span>$txtSrc</span>",$des);} $des = htmlentities($des,ENT_QUOTES); if (strlen($des) >= 1000) { $des .= err("<a href=\"#\" onclick=\"showMore({$rowCnt}); return false;\">... [Show More] ...</a>"); $rowIdx = " id=\"show{$rowCnt}\""; } if ($txtSrc) {$des = str_replace(array('<span>','</span>'),array('<span>','</span>'),$des);} $txt .= " <td title=\"$tbl - $nam[$i]\"{$rowIdx}>$des</td>\n";} Link to comment Share on other sites More sharing options...
justsomeguy Posted May 29, 2011 Share Posted May 29, 2011 If the characters in the database are UTF or another multi-byte character set, you may need to use mb_strlen instead. Otherwise, I'm not sure what specifically about that code you don't think is working right. Link to comment Share on other sites More sharing options...
Err Posted June 2, 2011 Author Share Posted June 2, 2011 Thanks for your help, justsomeguy. I figured it out what was wrong. I wasn't accounting for the htmlentities, not the < itself but the < which makes a lot more characters. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.