Jump to content

text between slashes into radio buttons


kavkazi

Recommended Posts

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 . '" />  ';

} 

$html .= '</form>';

 

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

 

Thanks

Link to comment
Share on other sites

It sounds like you've already extracted the text between the hashes, and are left with the slash-separated text?

 

<?php
$i = 0;
$string = array('This/is/a', 'string/with', 'a/number/8', 'and/more/delineated', 'random/slashes/and/text');
foreach( $string as $val ) {
   foreach( explode('/', $val) as $v ) {
      echo "Var: " . trim($v) . ": <input type=\"radio\" name=\"place[$i]\" value=\"" . trim($v) . "\"><br>\n";
   }
   $i++;
   echo '<br><br>';
}
?>

Link to comment
Share on other sites

yes, i've already extracted the text between the hashes.

 

The problem is, the text between the hashes doesnt always contain a slash. Sometimes it can be just a regular text.

 

For example:

 

$string = array('Hello', 'string/with', 'Hi Pals', 'and/more/delineated', 'random/slashes/and/text');

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.