Grischa
Grischa

Reputation: 80

HTML between own PHP function

Is it possible to put HTML code in the middle of the output of a PHP function?

The French() function deletes the last letter of $word_one and appends an apostrophe to it, when $word_two begins with a vowel.

the function

function French($word_one, $word_two) {
if(preg_match('~(.*)\bje$~ui', $word_one, $m) && preg_match('~^[aeéio]~ui', $word_two)) 
    return "{$m[1]}j'$word_two";   
if(preg_match('~^[-]~ui', $word_two)) 
    return "$word_two";            
return "$word_one $word_two";      
}

Code using the function:

for ($i = 0; $i <= 5; $i++) {
    echo                                        
  '<tr>'."\n".  
  '   <td><span data-text="'.French($pers[$i],  strip_tags($array[0][$i])).'" data-lang="fr" class="trigger_play">'.French($pers[$i], $array[0][$i]).'</span></td>'."\n".
  '   <td>'.French($pers[$i], $array[0][$i]).'</td>'."\n".    
  '</tr>'."\n".

Everything is now printed in one td. But I want to print it in three td's, the first one is ok, but in the second and third td, it should also use the French(..) function.

How can I split French($pers[$i], $array[0][$i]) between the two TDs and get the following?

'<tr>'."\n".
' <td><span data-text="'.French($pers[$i],  strip_tags($array[0][$i])).'" data-lang="fr" class="trigger_play">'.French($pers[$i], $array[0][$i]).'</span></td>'."\n".
' <td>'.$pers[$i].'</td>'.
' <td>'.$array[0][$i].'</td>'.
'</tr>'."\n".

Upvotes: 0

Views: 86

Answers (3)

LSerni
LSerni

Reputation: 57378

You can modify the French() function to return a different string.

Or you can add a copy of the French() function that returns a different string and call it FrenchSeparated(), so that you don't risk breaking old code that relies on current French() return format.

I'll now describe a third option: modifying the French() function to return not a string but an array (if one only did this, one would then also need to "refactor" all old code where it uses this function), but to only do this when called by the new code.

For portability, let's suppose that French() now only takes two arguments. You modify it to accept three:

function French($word_one, $word_two, $return_array = false) {
  // Basic, example implementation of the function - NOT the real one
    if (in_array(substr($word_two, 0, 1), array('a', 'e', 'i', 'o', 'u')) {
      return substr($word_one, 0, -1) . "'" . $word_two;
  }
  return $word_one . " " . $word_two;
}

You modify this to get an array, and return it joined:

function French($word_one, $word_two, $return_array = false) {
    if (in_array(substr($word_two, 0, 1), array('a', 'e', 'i', 'o', 'u')) {
      $coo = array( substr($word_one, 0, -1)."'", $word_two);
  } else {
      $coo = array ($word_one.' ', $word_two);
  }
  return join('', $coo);

Having supplied a default value ("false") for return_array, whenever you call the function with two parameters and omit the third, the function will receive three parameters, and the third will be set to false.

The function now returns the same as before. You now add a check for the new code, where return_array will be true:

function French($word_one, $word_two, $return_array = false) {
    if (in_array(substr($word_two, 0, 1), array('a', 'e', 'i', 'o', 'u')) {
      $coo = array( substr($word_one, 0, -1)."'", $word_two);
  } else {
      $coo = array ($word_one.' ', $word_two);
  }
  if ($return_array) {
      return $coo;
  }
  return join('', $coo);
}

The old code now needs no modifications: the function will work as before with two parameters, and your old code only ever uses two. In the new code, when you pass a third parameter with the value of true, the function will return both word separately.

So you can do:

 list($one, $two) = French($pers[$i], $array[0][$i], true);

 echo "<tr><td>{$one}</td><td>{$two}</td></tr>";

Upvotes: 1

sabkaraja
sabkaraja

Reputation: 342

If "French" is your own function, try this:

function French($a, $b, $glue='') {
   //do your logical stuff
   return $result_a . $glue . $result_b;

}

and wherever you call, do this:

echo '<tr>\n<td>' . French($pers[$i], $array[0][$i], '</td><td>') .'</td>\n</tr>';

Upvotes: 0

Gerton
Gerton

Reputation: 686

'<tr>'."\n".
' <td>'.$pers[$i].'</td>'.
' <td>'.$array[0][$i].'</td>'.
'</tr>'."\n".

Don't know if this fixes your issue, since your question was pretty vague.

But man, you should really keep your quotation marks in check, not calling functions by adding them in single quotationmarks, etc. it's horrible sorry.

in the French function change :

$output = $word_one . ' ' . $word_two;

to

$output = '<td>' . $word_one . '</td><td>' . $word_two . '</td>';

and your php code to :

'<tr>'."\n"
.French($pers[$i], $array[0][$i]).
'</tr>'."\n";

Upvotes: 2

Related Questions