Checking string for multiple values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ziycon
    Contributor
    • Sep 2008
    • 384

    Checking string for multiple values

    I have a string passed in and I want to check validate it to make sure that multiple values don't exist at the start, the below would only check the entire string for the value??
    Code:
    if(strstr($string,array("value1","value2","value3")) {
      ...
    }
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    What do you mean by 'start'?

    Comment

    • ziycon
      Contributor
      • Sep 2008
      • 384

      #3
      Say a link is entered like bytes.com I want to check it to see if it has www. before it and if not to add it or another example would be say http://www.bytes.com was entered I want to check and remove the http://.

      I'm looking to validate the input so only www.bytes.com is kept.

      Does this make sense?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        it does. it also makes sense to use Regular Expressions for that.

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Hey.

          You could use the substr function to fetch parts of the string and see if they are, or are not, what you expect them to be.

          Like, to remove http:// from the start of a string:
          [code=php]if(substr($inpu t, 0, 7) == 'http://') {
          $input = substr($input, 7);
          }[/code]

          Or, as Dormilich suggested, you could use a regular expression. It's a lot more complex, and may be overkill if this is as simple as removing http:// from URLS, but may be better if you are doing something more complex.

          For example, this could be used with the preg_match function to split an URL into individual parts, which can then be verified and assembled into the format you want.
          [code=php]$regexp = '#(https?://)?(([^\.]+\.)+)?([^\.]+)\.([^/]{2,4})#i';[/code]
          But, like I say, this is a bit more complex, so it may be a bit much for simple tasks that substr can take care of.

          Comment

          Working...