Lowercase text from database

Hy I have problem when retrieve data from database. I explode data but i can’t get lowercase.
Here’s the code

    <?php
    /*
        This get records from database
   */
    
$items = Tekst::getReccords();

  /*
        this is function to explode data a get words with -
       example:text from table These Is A Test 
       when exploded get  These-Is-A-Test 
        I try everything to get small text strtolower doesnt' work
 */
function linkovi($tekst)
{
   //$tekstovi = strtolower($tekst);
    $links = explode(" ", $tekst);
    foreach ($links as $key => $value) {
       echo strtolower ($value)."-";
    }   
    
    
}
?>
<ul>
    <?php foreach ($items as $item): ?>
    <li><p><?php  
        linkovi($item->getNazivTeksta());
   
    ?>
            
      </li>
    <?php endforeach; ?>
    
</ul>

One thing that might help - you don’t want a space after strtolower in this line: echo strtolower ($value)."-";

Try to avoid echo in functions/methods - you may return stuff, assign stuff, throw exceptions etc but not generate output

<?php

$results = array(
    'These Is A Test ',
    'These Is another Test ',
);

function strtolowerArray($words){
    $arrayWords = explode(' ', $words);
    $finalResult = array();
    foreach($arrayWords as $word) {
        if( trim($word) !== '' ) {
            $finalResult[] = strtolower($word);
        }
    }
    return implode('-', $finalResult);
}

foreach($results as $result) {
    echo '<br>' . strtolowerArray($result);
}

Still not working i create another code it’s same a get value but with uppercase.

<?php //get data from database $items = Tekst::getReccords(); //function function toLowercase($text) { $newText = explode(" ", $text); $impl = implode("-", $newText); $finish = strtolower($impl); return $finish; } ?>
     <?php foreach ($items as $item): ?>
    
     <li><p><?php  
    
              echo toLowercase($item->getNazivTeksta());
    
    
    ?>
    
    
    
       </li>
    
     <?php endforeach; ?>
    

It’s working when a manually add text example:

<?php 
function toLowercase($text)

>      {

        $newText = explode(" ", $text);

      $impl = implode("-", $newText);

       $finish =  strtolower($impl);

         return $finish;

>      }

$array = array("Hello World", "Hy There"');

<ul>

   <?php foreach ($items as $item): ?>

   <li><p><?php  

>               echo toLowercase($array);


  ?>



    </li>

    <?php endforeach; ?>



</ul>



?>

I get
hello-world
hy there

True only when that isn’t their stated purpose. The number of functions which actually echo something out should be very few. I’ve written more than one application with only one print statement in the entire thing.

But if I have a function called printArray() I should expect to see something echoed out.

It is not working manually because there is missing hyphen between “hy there”.

Try this and change the $MODE to 0,1 or 2:

<?php 
function toLowercase($text, $MODE=0)
{
  $newText = explode(" ", $text);
  $impl    = implode("-", $newText);

switch( $MODE ):
  case 0:  
    $impl = $impl;
  break;

  case 1:
    $impl = htmlspecialchars( $impl );
  break;

  case 2:
    $impl = strip_tags( $impl );
  break;
endswitch;

  return  strtolower($impl); //  mb_strtolower($impl)
}//

  $items = array
  (
    "Hello World", 
    "Hy There", 
    "Hy<br />There\n\tAgain",
   );

  foreach ($items as $item): ?>
    <li>
  	<p>
          <?php 
  	    $MODE = 1;
  	    echo toLowercase($item, $MODE); 
  	?>
    </p><!-- MISSING ??? -->
   </li>
  <?php endforeach; ?>
</ul>



echo toLowercase($array); ?! :slight_smile: toLowercase should get a string.
You need

  1. foreach ($array as $item) // or items, in case you need to work with your database items
  2. toLowercase($item) – the string item, not the main array

You make your own purpose. Besides when you design a template engine (ie: smarty::display), I cannot imagine a method that should echo instead of return. It’s about architecture

Debug library dump statements.

They aren’t going to come up often, but they do come up.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.