-1

I found on the web a lots of complicated functions and options to see if the string starts with X this all where to complicated or to big how Can I do it in the fastest way for this Pseudocode

if price != startswith $ or €
    echo "<td>Free</td>"
else 
    echo "<td>"Price"</td>"

I only wanna check if there is a Dollar or Euro Sign as first char if not echo free else price

  • 2
    Checking text for a dollar/euro sign sounds like a horrible way to check for a price. What if the price doesn't have a sign? Does that magically make it free? – Jonathan Kuhn May 13 '15 at 17:47
  • `if price` coming from where? – Funk Forty Niner May 13 '15 at 17:50
  • Let us know what you have tried already, and we can help you fix your code. It lets us at stackoverflow know you put some attempt at research into your question. –  May 13 '15 at 17:50
  • possible duplicate of [Convert a String into an Array of Characters](http://stackoverflow.com/questions/2768314/convert-a-string-into-an-array-of-characters) – Machavity May 13 '15 at 17:51
  • Pregmatch is the function you want to use for this. http://php.net/manual/en/function.preg-match.php –  May 13 '15 at 17:53
  • @DavidEugenePeterson Why? Not only will you incur the overhead of a function but you will dogpile the overhead of a regex on top of that. Hanky's solution is the simplest and easiest to implement. Use regex for complex string matches only – Machavity May 13 '15 at 17:55
  • 1
    Voted to close as unclear, based on we don't know where your "price" is coming from. – Funk Forty Niner May 13 '15 at 17:56

2 Answers2

1

You can do

if($price[0]=="$" || $price[0]=="€")
   //not free, for whatever business logic
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • I can't vote anymore for today, but I would give you a vote if I still could for today. (Maybe you also want to add a little explanation what you do here and also that you can use/access a string as it would be an array ([link](http://php.net/manual/en/language.types.string.php#language.types.string.substr))) – Rizier123 May 13 '15 at 18:02
  • 1
    @Rizier123 Im not 100% stupid ;) but maybe for other users it would be usefull if they find this either I think both of them works fine. Thanks anyways! – OfficialJoe May 13 '15 at 18:30
  • @OfficialJoe Maybe not every one which will read this answer will know that you can use a string as an array, so that might be quite interesting for those users :) – Rizier123 May 13 '15 at 18:32
1

One option is using strripos() to check to see if the character is in position 0 of the string.

$price = '$2.00';

if(strripos($price, '$') === 0 || strripos($price, '€') === 0) {
    // do stuff 
} else {
   // do other stuff
}

You can also use the strrpos() function in the same way.

One of the reasons I use these two functions for something like this is to make sure that I am only dealing with one item in the string in the position I am looking for. If the last dollar sign's location is greater than 0 it means that I am not looking at a price string as I would expect it to be.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • Thanks, I cannot upvote this yet but this is the awnser Iam gonna use.. Thanks for your help! – OfficialJoe May 13 '15 at 17:58
  • you should use `===` instead of `==` – Gustek May 13 '15 at 18:07
  • if $ or € signs will not be in string the return value for `strripos` will be `false` and in php `false == 0` is `true` but `false === 0` will be `false`. there is note about it in documentation you linked to. – Gustek May 13 '15 at 18:10
  • Good catch @Gustek, I have made the changes. – Jay Blanchard May 13 '15 at 18:12
  • This is an over-kill IMHO, when you know you only have to check the first character then why tell a function to parse a whole string 2 times and find position of 2 characters. Why not just check the first character? – Hanky Panky May 14 '15 at 05:21
  • I agree @Hanky웃Panky and that is why I only offered it as an option. – Jay Blanchard May 14 '15 at 11:56