TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Help with function stristr() (http://www.talkphp.com/general/2943-help-function-stristr.html)

pipesportugal 06-10-2008 11:52 AM

Help with function stristr()
 
Hello everyone,

I am using the above mentioned function in the following way:

PHP Code:

$text0 "int(7)";

$text1 stristr($text0'(');
echo 
$text1// ok! outputs (7)

$text2 stristr($text0'('true);
echo 
$text2// I wanted to retrieve the word -> int 

I am getting the following error on the second instruction:
Warning: Wrong parameter count for stristr()

I've checked the manual and everything seems to be ok, so what am I doing wrong ?

Can someone help?
pipesportugal

Salathe 06-10-2008 12:26 PM

The third argument (before_needle) is only available with PHP 5.3.0 and upward.

An equivalent snippet would be something like:
PHP Code:

$text2 = (FALSE !== ($pos strpos($text0'('))
       ? 
substr($text00$pos)
       : 
FALSE


pipesportugal 06-10-2008 02:08 PM

Hi Salathe,

I really was not expecting anyone to reply so soon, as my idea is(was...) that most talkphp members were on the left side of the Atlantic Ocean therefore by time I've sent this thread through, I thought everyone was still spleeping.

As You can (easily) imagine my routine was reading the result of a mySQL query "SHOW COLUMNS FROM table" and then I was using this routine to desintegrate column 2: example: int(7) into: $type=int and $length=7, as I want to develop a simple form generator class(by my own).

In the meanwhile I changed approach and went by this road:
$this->query = "SELECT COLUMN_NAME, DATA_TYPE, NUMERIC_PRECISION, NUMERIC_SCALE, CHARACTER_MAXIMUM_LENGTH, COLUMN_COMMENT from information_schema.columns where table_name = '$table'";

This sql instruction gives me directly the fields that I was looking for.

Regarding Your precious advice, those lines execute its purpose but I must confess that it's difficult for me to "read" Your php lines, as I have never done anything like that before. Looks complicated...from rookie my point of view. Some instructions how to read them in plain English would be nice...

Thanks for caring,
Cheers,
Jose.

delayedinsanity 06-10-2008 03:01 PM

Salathe doesn't sleep. He's out there, maintaining the balance.

It might be harder to read if you're not first familiar with the ternary operator. It's a really handy shorthand for a basic IF statement.

PHP Code:

/* in english */
if (statement evaluates true) ? 'then do this' 'otherwise do this';

/* in PHP */

// echo's "$somevar is set" if true
echo (isset($somevar)) ? '$somevar is set' '$somevar is not set';

// sets $isSet to true, if true
$isSet = (isset($somevar)) ? true false;

// sets $answer to "$somevar is less than or equal to $othervar" if true
$answer = ($somevar <= $othervar) ? '$somevar is less than or equal to $othervar' '$othervar is less than $somevar'

So what Salathe has done;
If $pos = strpos($text0, '(')) doesn't return false, than that means '(' was found somewhere in $text0 and the statement evaluates to true. If it evaluates to true, the ternary executes the code found after the ? and before the :, which in this case returns a substr of $text0, from the start of the string to the position where the first ( was found. If it evaluates to false, it executes the code found after the : and before the end of the block, which in this case would return a value of FALSE.
-m

sketchMedia 06-10-2008 04:26 PM

Salathe has made use of the ternary operator, it works like this:

( expression) ? ( expression2 ) : (expression3)

If expression is true, then PHP will evaluate to ( expression2 )
If expression1 is false, then it evaluates to ( expression3 )

To help you understand it further it can be rewritten like so:

PHP Code:

if(($pos strpos($text0'(')) !== FALSE ){
    
$text2 substr($text00$pos);
}else{
    
$text2 FALSE;


So, first PHP creates '$pos' and assigns the result of 'strpos' to it, then it functions like a regular IF, so if $pos is not equal to FALSE, or $pos not of the same type (which is what the !== comparison operator means, this is used because it will return an int on success and a bool false if it failed) then assign result of substr to $text2, else assign it FALSE.

As you can see using the ternary is smaller but at the same time can be tricky to read, especially if you nest them, which you shouldnt do, not unless you want to give yourself a headache, look at this snippet posted by salathe in another post:
PHP Code:

echo chr(16 + ((strlen('talkphp') + 1337) >> ((true === false
     ? ((
'a' === chr(0x61)) ? 'talkphp' 'php'
     : ((
767 & (405 - (pow(12* ('0' 
     
false true))))))))), 
     
chr('happy' 0x29 0x28); 

edit : damn delayedinsanity beat me to it, i must learn to type faster!

delayedinsanity 06-10-2008 05:34 PM

Yeah, but you explained it a lot better. ;-) -m

Salathe 06-10-2008 05:40 PM

Bonus points to sketch for finding my example of "when ternary goes bad!". :-)

pipesportugal 06-11-2008 08:14 PM

Dear Friends,

Understood the basic about the ternary operator and some tests made with sucess.

Are there any limit to the "thank You" I can give to my forum colleagues?

I am new but I suspect I am gone give quite a few in the next times.

Thank You all,
pipesportugal


All times are GMT. The time now is 05:49 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0