homepage Welcome to WebmasterWorld Guest from 107.22.45.61
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Accredited PayPal World Seller

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Regex Remove Non-Alphabetical characters from beginning of string
webfoo




msg:4609409
 12:05 am on Sep 13, 2013 (gmt 0)

I am trying remove any characters from the beginning of the string, except for letters A-Z and a-z. In other words, I need to make sure that the string begins with a letter.

Regular Expressions are not my strength by any stretch of the imagination, but here's what I've managed to hack up so far.

$str = preg_replace( "/^[^A-Za-z]*/", '', $str );


For some reason it isn't removing the characters. Any thoughts?

 

JD_Toims




msg:4609411
 12:13 am on Sep 13, 2013 (gmt 0)

You're really close to having it working.

* = 0 or more of the preceding
+ = 1 or more of the preceding

With the negative match you need to check for 1 or more characters not a-z.

<?php

$str="123abc";
$str = preg_replace( "/^[^A-Za-z]+/", '', $str );
echo $str;

echo '<br>';

$str="abc1def";
$str = preg_replace( "/^[^A-Za-z]+/", '', $str );
echo $str;

?>

webfoo




msg:4609413
 12:24 am on Sep 13, 2013 (gmt 0)

Thanks for the reply! Even using + it is still not removing the characters I need it to remove. In this particular case, the string begins with "16:13 (3)"

JD_Toims




msg:4609414
 12:28 am on Sep 13, 2013 (gmt 0)

Try emptying your cache [especially if you're using IE], because even when I drop the characters you provided in I'm getting the correct string.

Here's some more tests I just ran to make sure.

<?php

$str="16:13 (3) 123abc";
$str = preg_replace( "/^[^A-Za-z]+/", '', $str );
echo $str;

echo '<br>';

$str="16:13a(3)b123abc";
$str = preg_replace( "/^[^A-Za-z]+/", '', $str );
echo $str;

echo '<br>';

$str="abc1def";
$str = preg_replace( "/^[^A-Za-z]+/", '', $str );
echo $str;

?>

/test.php results:
abc
a(3)b123abc
abc1def

[edited by: JD_Toims at 12:29 am (utc) on Sep 13, 2013]

webfoo




msg:4609415
 12:29 am on Sep 13, 2013 (gmt 0)

Disregard my last! I found the problem to be elsewhere in the code. Your suggestion of the + instead of * worked! Thank you so much for your help. Hopefully one of these days I'll get around to learning regex myself.

JD_Toims




msg:4609417
 12:32 am on Sep 13, 2013 (gmt 0)

Glad you got it sorted out!

Regexes take quite a while to learn -- I've been writing them for years and still find areas I can improve in, so I totally understand being too time-challenged to get to them and really sit down and figure out what makes them tick. It's tough enough to keep up with everything else for a site, so glad I could help!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved