PHP Content Manipulation

admin

Administrator
Staff member
Good day all,

I'm having a bit of a challenge manipulating some dynamic content; I hope someone can assist.

I have a PHP page that is delivering content from a db field, but I need it to only display the first 4 characters of the information.

Here's an example:

The field in the database might store something like this:
way1-2020

I need the PHP page to display the first 4 characters of this information:
way1


Can anyone assist?Yep.. just use this


$length = "4";

$sql = "SELECT LEFT([column_name], $length) AS [column_name] FROM [tablename] WHERE [conditions]";

$result = mysql_query($sql);
$column_name = mysql_fetch_array($result);

echo($column_name);


The bolded bit is the character length.

Hope this helps :)not sure that will work Jolly. if they don't know what the field name will be then you can type it in. column_name should be field_value. I beleive he is looking for the field not the column.

so query everything selects everything form the db then you could just use the substr()

$sql = "SELECT * FROM [tablename]";

$result = mysql_query($sql);
$column_name = mysql_fetch_array($result);

$string = substr($column_name["field"],0,4);

then string will be the way1 you are looking for.I just wanted to thank you two for your help.
I spent hours Friday trying to figure out what only took minutes with your assistance.

Thanks again!
 
Back
Top