0

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".

3 Answers 3

2
'<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";
Sign up to request clarification or add additional context in comments.

6 Comments

I had corrected the second code. I want to use the function, but it should print .French($pers[$i], $array[0][$i]). in two tds, first $pers[$i] and in the second $array[0][$i]
What does the .French(...) function returns?
for example "j'ai" or "je suis" (stackoverflow.com/questions/25594446/…)
added possible fix for you.
Thank you. I have add the function, like I use it now in the question and and an extra td, because actually I use it two times (one time for Google TTS with only a button and than printed it. I don't want to use html code every time, when I use the function.
|
1

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>";

Comments

0

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>';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.