Hello, can someone please help me with this string? Right now, it will validate keywords separated by commas, lowercase only, and any two digit numbers by themselves also separated by commas. How do I further modify it to permit /up to/ two keywords, trimming any extra spaces that might be entered?

<?php if (preg_match('/^(([a-z]+|\d{1,2})\s*,\s*)*([a-z]+|\d{1,2})$/', $string)) { [do something] } else { [do something else] ?>

what might be easier than usign that huge ugly regular expression is if you first break up your keywords

I have not tested any of this so it still might need a slight amount of tweaking but this should give you the idea you need to get going from here. If you want to get rid of ALL spaces not just trim a keyword then replace the
$keywords[$i] = trim($keywords[$i]); with this instead
$keywords[$i] = ereg_replace(" ",$keywords[$i]);

//this will seperate every keyword into an array
$keywords = explode("," , $string);

//next let's trim any whitespace
for ($i=0; $i<sizeOf($keywords); $i++)
     $keywords[$i] = trim($keywords[$i]);

$i = 0;
$validKeyWord = true;
while (sizeOf($keywords) < $i && $validKeyWord)
{
     if (ereg("^([a-z]+)|[0-9]{2,2})$", $keyword[$i++]))
        $validKeyWord= false
}

if ($validKeyWord)
{
     if (sizeOf($keywords) > 2)
     {
        echo "error! you are limited to only 2 keywords";
        do_something_with_this_function($keywords);
     }
     else
     {
       echo "you have 2 keywords or less, good job";
       do_whatever_your_site_does_with_keywords($keywords);
     }
}
else
{
     echo "you did not enter a proper keyword, please try again";
}

Thanks for working on this! :idea:

I seem to be getting an error, I am not sure how to fix (it looks like there may be an extra ")" or something on line 18: if (ereg("^([a-z]+)|[0-9]{2,2})$", $keyword[$i++])) <-- ?, but still gives a parse error upon removal:

<?php
$keywords = "test,green,green,fun test ,trying,work work";

//this will seperate every keyword into an array
$keywords = explode("," , $string);

//next let's trim any whitespace
for ($i=0; $i<sizeOf($keywords); $i++)
     $keywords[$i] = trim($keywords[$i]);

$i = 0;
$validKeyWord = true;
while (sizeOf($keywords) < $i && $validKeyWord)
{
     if (ereg("^([a-z]+)|[0-9]{2,2})$", $keyword[$i++]))
        $validKeyWord= false
}

if ($validKeyWord)
{
     if (sizeOf($keywords) > 2)
     {
        echo "error! you are limited to only 2 keywords";
        do_something_with_this_function($keywords);
     }
     else
     {
       echo "you have 2 keywords or less, good job";
       do_whatever_your_site_does_with_keywords($keywords);
     }
}
else
{
     echo "you did not enter a proper keyword, please try again";
}
?>

One thing I see, this:

if (ereg("^([a-z]+)|[0-9]{2,2})$", $keyword[$i++]))
        $validKeyWord= false

should probably be:

if (ereg("^([a-z]+)|[0-9]{2,2})$", $keyword[$i++]))
     {
        $validKeyWord= false;
     }

Dance

That fixed the error. I feel i'm a big step closer! This is much better than the original expression I brought going into this, at least with this I can see what's going on better. I know all those print strings I added in my code below look stupid, but it's helping me learn and test this out in the process. As I go along I can see exactly how it's outputting everything. :)

I have these questions:

1) It seems to be allowing uppercase and special characters.

2) It will allow more than two words in a keyphrase
........ so, "cool,word word,22" [is good],
........ not, "cool,word word word,22"

3) Finally, it should just allow (up to) two digit numbers to pass:
........ '2,33,44' is fine,
........ '2 2,33 33,444 444 444' should not clear this validation

Some other examples of what should/should not clear:

blue,green green,93 (good)
green green,blue blue,10 (good)
10,32,green (good)
10,32 32,green (no)
10,green green,32 (good)
blue green green,10,30 (no)

Of course, no uppercase or special characters anywhere.

One last thing, I would rather have this output the finished product back into a comma delimited string, not an array so I can pump it into a database like that.

Thanks so much

Julia.

<?php
$string = "cool,cool too,22";

//this will seperate every keyword into an array
$keywords = explode("," , $string);

//next let's trim any whitespace
for ($i=0; $i<sizeOf($keywords); $i++)
     $keywords[$i] = trim($keywords[$i]);

$i = 0;
$validKeyWord = true;
while (sizeOf($keywords) < $i && $validKeyWord)
{
     if (ereg("^([a-z]+)|[0-9]{2,2})$", $keyword[$i++]))
        $validKeyWord= false;
}

if ($validKeyWord)
{
     if (sizeOf($keywords) > 3)
     {
        echo "error! you are limited to only 3 keywords";
?><br><?php
?>:<?php echo $keywords[0]; ?>:<br><?php
?>:<?php echo $keywords[1]; ?>:<br><?php
?>:<?php echo $keywords[2]; ?>:<br><?php
     }
     else
     {
       echo "you have 3 keywords or less, good job";
?><br><?php
?>:<?php echo $keywords[0]; ?>:<br><?php
?>:<?php echo $keywords[1]; ?>:<br><?php
?>:<?php echo $keywords[2]; ?>:<br><?php
     }
}
else
{
     echo "you did not enter a proper keyword, please try again";
?><br><?php
?>:<?php echo $keywords[0]; ?>:<br><?php
?>:<?php echo $keywords[1]; ?>:<br><?php
?>:<?php echo $keywords[2]; ?>:<br><?php
}
?>

Hello, can someone please help me with this string? Right now, it will validate keywords separated by commas, lowercase only, and any two digit numbers by themselves also separated by commas. How do I further modify it to permit /up to/ two keywords, trimming any extra spaces that might be entered?

<?php if (preg_match('/^(([a-z]+|\d{1,2})\s*,\s*)*([a-z]+|\d{1,2})$/', $string)) { [do something] } else { [do something else] ?>

Hi

I just started working with preg_match too.

[a-z] is lower case
[A-Z] is upper case
[a-zA-Z] upper and lower case

If I am not mistaken {1,2} is limiting you to two two characters seperated by comma.

Here is an article to look at.

Now what's that really do?

/[^a-zA-Z0-9\.\-\ß\ä\Ä\ü\Ü\ö\Ö\ ]+$/


The slashes "/" and "/" are delimiters, "^" marks the start of string or line and the Dollar sign "$" the end of the string, or line. The plus-symbol "+" means required.

Knowing what the special characters mean, it actually says the following: A string, from start to finish, may contain this characters (a to z (lower case), A to Z (upper case), the numbers from 0 to 9, a dot ("."), a hiven ("-") and the special characters ä, ö ü (both upper and lower case) and space (" ")), and these characters only.

preg_match() is a case sensitiv function, which means it treats "a" and "A" differently. I included upper ("A-Z") and lower case ("a-z"). So called "special characters" (Special, because they have another meaning in PHP as well. But that's another story.) have to be escaped, which means you write a backslash in front of it. For example: \- (the hiven) or \. (the dot). Other special characters are: "^[$()|*+?{\".

The other two functions are self explanatory, as they check only for numbers, and numbers and space ("\ ").

Hope this helps.

:mrgreen:

BUMP...

Hi, same problem... I have a piece of code below:

// Check our data
if (preg_match('/[^a-zA-Z0-9]/', $new_pagealias)) $error = "Invalid page alias. Use only normal characters.";
if (preg_match('/[^a-zA-Z0-9]/', $new_pagekeywords)) $error = "Invalid page keywords. Use only normal characters.";
if (!$new_pagealias) $error = "Page alias is missing";
if (!$new_pagename) $error = "Page name is missing";
if (!$new_pagetitle) $error = "Page title is missing";

This should check if $new_pagealias & $new_pagekeywords contain invalid characters, other than a-bA-B0-9 etc which works fine.

Well. The first one which checks the page alias works fine. The second one to check the keywords, doesn't. If I post a var to it containing the string '@' it will go through it and enter it into the database, same goes for the string "holiday, sunshine, ' " My HTML page will echo the meta etc ending with: holiday, sunshine, ' '/>

This ends the tag early which will error some browsers, plus I just want it to be perfect and not have to echo invalid keywords.

Plus I need the preg_match for $new_pagekeywords to validate and allow commas.

At the moment if I put the string "My house!" through alias & keywords, it will error for alias, but if I change alias to "house' and leave keywords as "My house!" it won't define $error & stop the SQL input.

Help? :(

Thanks,
Josh

Comments
Don't bump 5 year old threads. Create a new thread.
This article has been dead for over six months. Start a new discussion instead.