Php Select Text Starting At Part Of String
- Forums
- PHP
- Php Select Text Starting At Part Of String
i have a simple script i am trying to create. but its very repetitive and i want to be able to select and remove some text starting at a certain point in the string. php select certain text from a string [2100], Last Updated: Mon Jun 24, 2024
Williever
Sun Sep 05, 2010
1 Comments
1008 Visits
i have a simple script i am trying to create. but its very repetitive and i want to be able to select and remove some text starting at a certain point in the string.
for example if i have a string like this:
$foo = "whats your favorite color: a)red b)blue c)green";
$questions= "whats your favorite color:";
$answers= "a)red b)blue c)green";
as you can see from my $foo string, i want to be able to break it up into two separate string one to be the $question and another to be the $answers
how can i do this?
<?php $Question = "whats your favorite color: a)red b)blue c)green"; $Separator = 'a)'; function SplitString($Haytack, $Needle){ $Result = array(); if(strstr($Haytack, $Needle)){ $Result['Answer'] = strstr($Haytack, $Needle); $Result['Question'] = str_replace($Result['Answer'],'',$Haytack); return $Result; }else{ return false; } } if($Check = SplitString($Question, $Separator)){ echo '<PRE>';print_r($Check);echo '</PRE>'; }else{ echo 'error, did not find <b>'.$Separator.'</b> in <b>'.$Question.'</b>'; } ?>OUPUT:Array ( [Answer] => a)red b)blue c)green [Question] => whats your favorite color: )https://www.webune.com/forums/20100905cgcc.html