0

Hi,

I need to analyze a string and get the text between, before, and after the forward slashes..

The string will always look something like this, but will vary:

$str = "I will go to the store with <# one/two/three #> people."

Then I need to create a form with radio buttons for each choice. In this case (one, two, or three).

The text between the <# ... #> could be different every time, and with uknown amount of forward slashes.

Here is what I have so far but it doesn't work.

Code to find if there are slashes:

preg_match_all( '/([/]+)/',$str,$matches);

Code to create html form ..

$i = 0;
$html  = '<form>';

foreach ($matches[0] as $match){

     if ($pos = strpos( $str,$match ) ) === false )
          continue;
     }

     $html .= '<input type="radio" name="place-' . $i . '" value=". $match . '" />&nbsp;&nbsp;';

} 

$html .= '</form>';

Its not working as needed. I'm not sure how to create radio button choices for the words (one, two, three).

Thanks

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by hielo
0
$str = "I will go to the store with <# one/two/three #> people<# four/five/six #>.";
if( preg_match_all( '![<]#([^#]+)#[>]!',$str,$matches) )
{
	$id=0;
	$prefix='place_';
	foreach($matches[1] as $group=>$str)
	{
		$parts=explode('/',$str);
		foreach($parts as $v)
		{
			++$id;
			$v=trim($v);
			echo PHP_EOL.'<input type="radio" id="'.$prefix.$id.'" name="'.$prefix.$group.'" value="'.$v.'" /><label for="'.$prefix.$id.'">'.ucfirst($v)."</label>";
		}
	}
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.