Jump to content

REGEX Help (I think?)


macwise

Recommended Posts

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 &trade; and &reg; 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
Share on other sites

If you really have "&reg;" 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
Share on other sites

If you really have "&reg;" 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 :P

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.