GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   Need help with PHP code (https://gfy.com/showthread.php?t=907480)

DigitalPimp 05-27-2009 04:32 PM

Need help with PHP code
 
Anyone know why the following does not return desired results and how to get it to?

Code:

$string = 'a,b,c,d';

if(eregi("(a)|(adf)|(c)",$string,$matchedtext)){
echo $matchedtext[1]; // This returns an "a" which I want
echo $matchedtext[2]; // This is empty, I want it to return a "c"
}


DigitalPimp 05-27-2009 04:40 PM

Code should have read:

Code:

$string = 'a,b,c,d';

if(eregi("(a)|(c)|(adf)",$string,$matchedtext)){
echo $matchedtext[1]; // This returns an "a" which I want
echo $matchedtext[2]; // This is empty, I want it to return a "c"
}


CS-Jay 05-27-2009 05:13 PM

who don't you do a print_r($matchedtext) to see what's in there?

Beaver Bob 05-27-2009 05:24 PM

not sure exactly what you are trying to do here, but take some time to read up on regular expressions in PHP and you should be able to figure it out.

http://www.regular-expressions.info/php.html
http://www.php.net/manual/en/function.ereg.php

DigitalPimp 05-27-2009 05:30 PM

Quote:

Originally Posted by CS-Jay (Post 15898059)
who don't you do a print_r($matchedtext) to see what's in there?

print_r($matchedtext) returns:

Array ( [0] => a [1] => a [2] => [3] => )

My understanding is matchedtext[0] is supposed to return the complete string matched, matchedtext[1] is supposed to return the substring which starts at the first left parenthesis, matchedtext[2] the next and so on. For some reason matchedtext[2] is not returning the letter "c" as I would expect.

DigitalPimp 05-27-2009 05:35 PM

Quote:

Originally Posted by Beaver Bob (Post 15898090)
not sure exactly what you are trying to do here, but take some time to read up on regular expressions in PHP and you should be able to figure it out.

http://www.regular-expressions.info/php.html
http://www.php.net/manual/en/function.ereg.php

I am trying to get the second substring "(c)" to get stored in the $matchedtext array. I will review those pages.

calmlikeabomb 05-27-2009 06:39 PM

You owe me a blow job.

It could be shorter, but that takes time, more thought and money.

PHP Code:

<?php

$string 
'a,b,c,d';

foreach(
explode(','$string) as $match) {
    if(
in_array($match, array('a','c'))) $matches[] = $match;
}

print_r($matches); // Here are your items.

?>


DigitalPimp 05-27-2009 07:07 PM

thank you!


All times are GMT -7. The time now is 12:38 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123