explode check

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

69 Replies - 3220 Views - Last Post: 09 June 2012 - 02:48 PM Rate Topic: -----

#1 ShannonJapanese  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 34
  • Joined: 02-June 12

explode check

Posted 05 June 2012 - 05:32 AM

I am wondering how I can check if a string contains or fits a criteria, for example if I post a string that needs that needs to be validated, it must contain " : ", if not I need to let the user now.

How is this possible? I know you use the explode function like so:
$newString = explode(" : ", $oldString);


But how can I check if the oldString contained that?

Is This A Good Question/Topic? 0
  • +

Replies To: explode check

#2 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 170
  • View blog
  • Posts: 608
  • Joined: 12-October 09

Re: explode check

Posted 05 June 2012 - 05:37 AM

You can use a regular expression if you just want to find out if it contains a pattern or not.

http://php.net/manua....preg-match.php
Was This Post Helpful? 0
  • +
  • -

#3 ShannonJapanese  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 34
  • Joined: 02-June 12

Re: explode check

Posted 05 June 2012 - 05:40 AM

Alright, so if I am looking for " : " I'd do:
if (preg_match(" : ", $string)) {
    echo "Your syntax is right.";
} else {
    echo "Please correct your syntax.";
}



Is that right?
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,232
  • Joined: 08-June 10

Re: explode check

Posted 05 June 2012 - 05:41 AM

depending on the complexity of a check, string functions are an alternative (e.g. you don’t need a RegExp to find a string like " : ", for this strpos() is sufficient)
Was This Post Helpful? 3
  • +
  • -

#5 ShannonJapanese  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 34
  • Joined: 02-June 12

Re: explode check

Posted 05 June 2012 - 05:44 AM

View PostDormilich, on 05 June 2012 - 05:41 AM, said:

depending on the complexity of a check, string functions are an alternative (e.g. you don’t need a RegExp to find a string like " : ", for this strpos() is sufficient)


So, I would be doing:
if (strpos(" : ", $string)) {
    echo "Yes.";
} else {

}


Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,232
  • Joined: 08-June 10

Re: explode check

Posted 05 June 2012 - 05:45 AM

that wouldn’t do because 0 is a falsy value.
Was This Post Helpful? 0
  • +
  • -

#7 ShannonJapanese  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 34
  • Joined: 02-June 12

Re: explode check

Posted 05 June 2012 - 05:47 AM

I think I got it working, I need the 2nd argument to be the needle, and the first parameter the haystack. :)

if (strpos($string, " : ")) {
    echo "Yes.";
} else {
    echo "No.";
}


Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,232
  • Joined: 08-June 10

Re: explode check

Posted 05 June 2012 - 05:48 AM

still, the problem remains. think of the following test string: " : foo"
Was This Post Helpful? 0
  • +
  • -

#9 ShannonJapanese  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 34
  • Joined: 02-June 12

Re: explode check

Posted 05 June 2012 - 05:49 AM

That's a good point. How do I resolve that? Sorry for being so nooby, never done this before.
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,232
  • Joined: 08-June 10

Re: explode check

Posted 05 June 2012 - 05:51 AM

like in the examples in the Manual.
Was This Post Helpful? 1
  • +
  • -

#11 ShannonJapanese  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 34
  • Joined: 02-June 12

Re: explode check

Posted 05 June 2012 - 05:54 AM

Do I need to set an offset of 1?
Was This Post Helpful? 0
  • +
  • -

#12 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,232
  • Joined: 08-June 10

Re: explode check

Posted 05 June 2012 - 05:56 AM

how does the first example code do the test?
Was This Post Helpful? 0
  • +
  • -

#13 ShannonJapanese  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 34
  • Joined: 02-June 12

Re: explode check

Posted 05 June 2012 - 05:57 AM

It uses === to check if it contains it or not.
Was This Post Helpful? 0
  • +
  • -

#14 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,232
  • Joined: 08-June 10

Re: explode check

Posted 05 June 2012 - 05:58 AM

so it would be certainly a good idea to use === for the comparison.
Was This Post Helpful? 0
  • +
  • -

#15 dlhorin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-April 12

Re: explode check

Posted 05 June 2012 - 06:01 AM

Try the following
if (strpos($string, ":") === false) {
    echo "No.";
} else {
    echo "Yes.";
}



Reasoning:
strpos can return 0 if : is the first character, or false if : is not present.
=== is therefore needed as it checks both value and type
Was This Post Helpful? 0
  • +
  • -

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »