Jump to content

[SOLVED] Displaying the first four characters in php.


Andrew R

Recommended Posts

Hi how do I only display a limited amount of characters in php? For example:

<? echo $row_users[‘name’]; ?>

 

Name = Robb_Bob.  How would I only display only display Robb from the database?  All names in the database are separated by _ .

 

Link to comment
Share on other sites

That won't work, since the quotes characters are wrong. You should be using normal single quotes ' not "smart" quotes ‘’

 

Also if the first string is longer than 4 characters, a better way would be to use the explode() function:

<?php
$tmp = explode('_',$row['name']);
echo $tmp[0];
?>

 

Ken

Link to comment
Share on other sites

I just re-read your post... are you saying you always want to display the part before the _ ? No matter what the length?

 

That would be easiest achieved by....

 

<?php

  $names = explode('_',$row_users[‘name’]);
  echo $names[0];

?>

Link to comment
Share on other sites

I just re-read your post... are you saying you always want to display the part before the _ ? No matter what the length?

 

Yeah most names in the database are four characters long but some are longer so I want to display everything before the _. 

 

How would have add something that this into the above code

$names = addslashes($riga[2]);

instead of the

<? echo $row_users[‘name’]; ?>

 

Thanks for the help.

Link to comment
Share on other sites

Sorry to bring this topic up again but how would I display everything after the _ if the name was Robb_Bob how would I display Bob.

 

Thanks for your help.

$var = "Robb_Bob";
$x = explode("_",$var);
echo $x[1];
//use print_r to display everything in the array, so you can see what explode does
//print_r($x);

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.