tags:

views:

36

answers:

1

I have a simple php file with a GET from a form to throw up some files from a directory. Quite simple code but I just need to strip from the string ANY characters that are NOT lowercase alpha. How would I go about doing that? (I'm a novice).

Here 'tis:

<?php $text = $_GET['text_string']; ?>

<form method="GET" action="index.php">Please enter some letters: <input type="text" name="text_string" value=""/> and hit <input type="submit" value="enter" />
</form>

<?php /* Split characters into an array */ $array = str_split($text); ?>

<?php foreach($array as $char) : ?>
   <img src="glyphs/<?php print ($char); ?>.jpg"/>
<?php endforeach ; ?>

Thanks!

+3  A: 

To lowercase (note that you also can use mb_strtolower for better charset handling, but in this case you're only going to keep ASCII chars anyway so strtolower is enough):

$text = strtolower($text);

To remove all non-alpha chars using preg_replace:

$text = preg_replace('/[^a-z]/', '', $text);
Emil Vikström
No matter how many times I see regex it *still* looks like magic... +1 =)
David Thomas
ah! Thanks Emil :-) After writing this post I realised that perhaps I should also convert UC to LC... do you know how I would do this?
Niels Oeltjen
Niels, I've updated my answer :-)
Emil Vikström
So quick!!! Thanks very much
Niels Oeltjen
Still creating glyphs j-z but here is the unstyled thing: http://alphabeta.nielsoeltjen.com/index.php
Niels Oeltjen