Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    34
    Thanks
    11
    Thanked 4 Times in 4 Posts

    PHP Code to check if phrase contains only numbers

    Hello,

    I need a PHP code using the preg_match() function to check if a phrase contains numbers. I also need another code to make sure a phrase only contains letters, numbers, hyphens or underscores. If anyone could make one that would be great.

  • #2
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    use the is_numeric function e.g. is_numeric($string); or preg_match('/[^0-9]/',$string); and for letters, numbers etc use preg_match('/[^0-9a-zA-Z-_]/',$string);
    Last edited by DJCMBear; 03-26-2010 at 02:30 AM.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • Users who have thanked DJCMBear for this post:

    Cloud Ghost (03-25-2010)

  • #3
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts

  • #4
    New Coder
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    34
    Thanks
    11
    Thanked 4 Times in 4 Posts
    @DJCMBear Thanks! Why did you remove the ^ out of the code though because I realized my code is only working with that.
    @kbluhm Thanks! That might be handy in the future. *bookmarks*

  • #5
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    i didnt know i did lol ill put it back in. and the ^ means not so if ^0-9 that if not a number.
    Last edited by DJCMBear; 03-26-2010 at 02:31 AM.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #6
    New Coder
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    34
    Thanks
    11
    Thanked 4 Times in 4 Posts
    Hello Again,

    I just added a tag system to my content management system. When I add a post with tags it will look like this in the database for example: red,blue,yellow,pink
    When I select the tags from the database I want to make them display on the page like this if you were to view the page source:
    Code:
    <a href="/index.php?tag=red">red</a>, <a href="/index.php?tag=blue">blue</a>, <a href="/index.php?tag=yellow">yellow</a>, <a href="/index.php?tag=pink">pink</a>
    Could someone help me with this and is it possible to use preg_replace() to do this?

  • #7
    Regular Coder
    Join Date
    Jan 2008
    Location
    Geneva, Switzerland
    Posts
    413
    Thanks
    12
    Thanked 29 Times in 29 Posts
    Not sure I understand what you want but...

    PHP Code:
    <?
    $sql 
    "SELECT tag from TABLE_TAGS";

    $result mysql_query($sql) or die('SQL Error !<br>'.$sql.'<br>'.mysql_error());
    $rows mysql_num_rows($result);
    $i=0;

    while (
    $i $rows) {

         
    $data mysql_fetch_array($result);

         echo 
    "<a href=\"/index.php?tag=" $data['tag'] . "\">" $data['tag'] . "</a>, ";

         
    $i++;

    ?>
    Of course, you will have to replace 'tag' and TABLE_TAGS with the correct names.
    Chuck Norris counted to infinity.
    Twice.

  • #8
    New Coder
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    34
    Thanks
    11
    Thanked 4 Times in 4 Posts
    Not exactly. That wouldn't work because the tags are saved in one row like this for example: red,green,blue,yellow And I need to code that will make each one a link and make them appear like this:
    red, green, blue, yellow

  • #9
    Regular Coder
    Join Date
    Jan 2008
    Location
    Geneva, Switzerland
    Posts
    413
    Thanks
    12
    Thanked 29 Times in 29 Posts
    Why not use EXPLODE then?
    Then a foreach loop should do the trick.
    Chuck Norris counted to infinity.
    Twice.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •