homepage Welcome to WebmasterWorld Guest from 54.237.213.31
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
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

5+ Year Member



 
Msg#: 4609407 posted 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

WebmasterWorld Senior Member Top Contributors Of The Month



 
Msg#: 4609407 posted 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

5+ Year Member



 
Msg#: 4609407 posted 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

WebmasterWorld Senior Member Top Contributors Of The Month



 
Msg#: 4609407 posted 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

5+ Year Member



 
Msg#: 4609407 posted 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

WebmasterWorld Senior Member Top Contributors Of The Month



 
Msg#: 4609407 posted 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.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved