Jump to content

Best practice for removing spaces, dashes, ampersands, special characters from a string?


hollyvalero
 Share

Recommended Posts

Hi!  I've got 3 page reference fields (multipage) used to tag database entries by audience, subject area, and resource type. These can be created on the fly, so you can ideally curate a special collection and have processwire generate pages, sorted displays, etc. 

Using Mixitup to create a nice sift and sort with multiple drop down menus. Mixitup doesn't like spaces, dashes, special characters - just letters. Rather than tell everyone, "don't use special characters!" I'd like to assume the worst and strip them out behind the scenes.  The option value and card class are both invisible, so as long as it's just letters it should work whether it's upper or lower case. 

 

For the drop down menu I need to generate something like this:

<option value='.ResumesJobs'>Résumés & Jobs</option>

 

And then the actual card display below will contain a matching ID:

<div class="mix  ResumesJobs">
---etc.
</div>

 

I've got the basics working... I can remove extra spaces. I can get rid of special characters. But I keep getting dashes and amp; in the displays.  Tried the sanitizer->pageName which didn't get rid of the amp. Sample of my sad code below... gather all pages listed under subjects... create a variable $submix for the title... then try to strip out spaces and characters:

 foreach($subjects as $subject) {

     $submix = $subject->title;
             $submix = str_replace(' ', '', $submix);
             preg_replace('/[^A-Za-z0-9\-]/', '', $submix);

     echo "<option value='.$submix'>{$subject->title}</option>" ;
     }
 

Is there a better single process? Sanitizer seems like overkill since this in internal stuff, not user input. Tried to html encode &amp; back to & and then strip spaces, characters, etc. but bogging down in errors and unable to get rid of dashes.

Would appreciate any insights.

 

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

I was all set to suggest that you do this:

image.png.57b76b537cd5fdeee6aed636ccc71988.png

but apparently the Sanitizer::translate option forces lowercase (https://github.com/processwire/processwire-issues/issues/643).

Here's another interesting thing I just found: https://github.com/processwire/processwire-issues/issues/644 which doesn't seem expected to me.

If Ryan agrees that the first one is a bug, then the alpha sanitizer with translate should result in this:

image.png.57b32eb58a22a447ffe6246941aff8a5.png

 

  • Like 3
Link to comment
Share on other sites

The sample page name is my working "worst case scenario" but there's no way of guessing what page titles will be. I've got multiple versions of this foreach loop...

$submix = $subject->title; (maps & geolocation, math, STEM, and a zillion others)

$resmix = $resourcetype->title; (videos, games, magazines, etc.)

$audmix = $audience->title;  (kids, taxpayers, college bound, whatever)

 

Assuming I might have to convert each title variable back to HTML, then remove "&amp;" or all special characters...
Then spaces... and dashes

I think caps and lowercase will work fine, however.

Link to comment
Share on other sites

1 minute ago, adrian said:

If you don't care about keeping the uppercase, then why not use the alpha with translate option that I posted above? Doesn't that take care of everything you need?

 

It will translate to lowercase, but if someone puts in "Frick & Frack" ... what happens to the & and the space?

Link to comment
Share on other sites

1 minute ago, hollyvalero said:

It will translate to lowercase, but if someone puts in "Frick & Frack" ... what happens to the & and the space?

They will be removed as you'll see in my example - isn't that what you want?

Link to comment
Share on other sites

Just now, adrian said:

They will be removed as you'll see in my example - isn't that what you want?

It is... just don't know how to code that... I haven't tried this... but something like this?

foreach($subjects as $subject) {

      $submix = $subject->title;
      $submix =  ($sanitizer->alpha('$submix', Sanitizer::translate) ) ;     
     echo "<option value='.$submix'>{$subject->title}</option>" ;
     }

 

 

 

 

Link to comment
Share on other sites

foreach($subjects as $subject) {
    echo "<option value='".$sanitizer->alpha($subject->title, Sanitizer::translate)."'>{$subject->title}</option>";
}

 

Check out the inspection of the select options and you can see it's working as expected.

image.png.46f5e25412fa2f47b2ce968b23f958a9.png

  • Like 2
Link to comment
Share on other sites

On 7/19/2018 at 4:43 PM, adrian said:

foreach($subjects as $subject) {
    echo "<option value='".$sanitizer->alpha($subject->title, Sanitizer::translate)."'>{$subject->title}</option>";
}

 

Check out the inspection of the select options and you can see it's working as expected.

image.png.46f5e25412fa2f47b2ce968b23f958a9.png

 

So this code:

 

$subjects = $pages->find("template=subject, limit=100, sort=title");
foreach($subjects as $subject) {
    echo "<option value='".$sanitizer->alpha($subject->title, Sanitizer::translate)."'>{$subject->title}</option>";
}

Does product the single name dropdown, lower case without the & or spaces. 

<option value='artandculture'>Art and Culture</option>
<option value='mapsampgeo'>Maps &amp; Geo</option>
<option value='Science'>
Science</option> 


I thought it was working, but realized I need to include a leading PERIOD in the option value before the name in order for mixitup to work... :


<option value='.mapsampgeo'>Maps &amp; Geo</option>
<option value='.artandculture'>
Art and Culture</option>


Is there a way using this sanitize string to add the "." back in or do I need to go back to 2-3 steps to launder stuff out?

 


 

 

 
 


 

 

 

 

 

Link to comment
Share on other sites

3 minutes ago, hollyvalero said:

Is there a way using this sanitize string to add the "." back in or do I need to go back to 2-3 steps to launder stuff out?

Just add the period in:

echo "<option value='.".$sanitizer->alpha($subject->title, Sanitizer::translate)."'>{$subject->title}</option>";

This will result in:

<option value='.mapsampgeo'>Maps &amp; Geo</option>

Is this what you are looking for?

Link to comment
Share on other sites

1 minute ago, adrian said:

Just add the period in:


echo "<option value='.".$sanitizer->alpha($subject->title, Sanitizer::translate)."'>{$subject->title}</option>";

This will result in:

<option value='.mapsampgeo'>Maps &amp; Geo</option>

Is this what you are looking for?

THAT'S IT! You're a genius!! Thank you!!

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...