macwise Posted August 31, 2010 Share Posted August 31, 2010 Ok, my first post here (earlier today) was a huge help, so here's a second... I have the following PHP code: $name = $row['NAME']; $nameTrimmed = $name; if ((strlen($nameTrimmed) > 20) && (strlen($nameTrimmed) > 1)) { $whitespaceposition2 = strrpos($nameTrimmed," ",5); $nameTrimmed = substr($nameTrimmed, 0, $whitespaceposition2); echo $nameTrimmed . "..."; } if ((strlen($name) <= 20) && (strlen($name) > 0)) { echo $name; } This is working as expected. However, many of the "name" fields in the DB contain ™ and ® strings, which inflate the count of each product title. Since I'm using the strrpos to limit the length of the longer product names (due to limited space in the layout), I would like to show as many full titles as possible. Many are being cut prematurely short due to the extra characters from the symbols. Do I need to run all this through a regex filter before I count it? If so, can anyone share an example of code I could try? Regex has never been my weak point, let alone my strong point. Or is there something else I haven't thought of? Any help is greatly appreciated! Link to comment https://forums.phpfreaks.com/topic/212144-regex-help-i-think/ Share on other sites More sharing options...
RussellReal Posted August 31, 2010 Share Posted August 31, 2010 try this regex.. preg_replace("/&[a-z]{2,5};/i",'X',$title); that SHOULD work.. however.. y ou could just pass over with html_entity_decode Link to comment https://forums.phpfreaks.com/topic/212144-regex-help-i-think/#findComment-1105510 Share on other sites More sharing options...
btherl Posted August 31, 2010 Share Posted August 31, 2010 If you really have "®" in your db, you might need to html_entity_decode() twice to get the actual character. Keep in mind that ® and ™ are not going to map to ascii characters though .. Instead of decoding, you might want to just replace "&...;" sequences with a single dummy charater, so you can get the length count correct. Then use the original string for displaying. Link to comment https://forums.phpfreaks.com/topic/212144-regex-help-i-think/#findComment-1105513 Share on other sites More sharing options...
RussellReal Posted August 31, 2010 Share Posted August 31, 2010 If you really have "®" in your db, you might need to html_entity_decode() twice to get the actual character. Keep in mind that ® and ™ are not going to map to ascii characters though .. Instead of decoding, you might want to just replace "&...;" sequences with a single dummy charater, so you can get the length count correct. Then use the original string for displaying. thats what I do in my first example with "X" but yes he does make a point about the ascii characters, I'm not a real guru with charsets or anything I just know regex n stuff Link to comment https://forums.phpfreaks.com/topic/212144-regex-help-i-think/#findComment-1105517 Share on other sites More sharing options...
macwise Posted August 31, 2010 Author Share Posted August 31, 2010 Awesome! Thanks! Here's what I ended up with (is this totally hacked looking or did I do this in an acceptable way?): $name = $row['NAME']; $nameTrimmed = $name; if ((strlen(preg_replace("/&[a-z]{2,5};/i",'X',$name)) > 16) && (strlen($name) > 1)) { $whitespaceposition2 = strrpos($name," ",5); $nameTrimmed = substr($name, 0, $whitespaceposition2); echo $nameTrimmed . "..."; } else { echo $name; } Seems to work great. Thanks again—I suck at regex! Link to comment https://forums.phpfreaks.com/topic/212144-regex-help-i-think/#findComment-1105521 Share on other sites More sharing options...
btherl Posted August 31, 2010 Share Posted August 31, 2010 That looks right to me. As long as you only need a single level of decoding. You might want to check the result of that preg_replace() to make sure it is what you expect it to be. Russel, if you're replacing with X then you don't need to worry about character sets luckily Unless there are some html entities that appear as more than one character, that's a possibility .. but I can't think of any. Link to comment https://forums.phpfreaks.com/topic/212144-regex-help-i-think/#findComment-1105869 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.