Jump to content

Need some help... Using str_replace


earunder

Recommended Posts

Oki,

 

Here is what I'm trying to do....

 

I've set the variable, $model, from an xml sheet and would like to alter the variable to match the value from a database.

 

For example...

 

$model will equal the xml value of "ASTRA CDTI LIFE"

 

sometimes model will equal "ASTRA ELITE"

sometimes model will equal "VECTRA CLUB"

and so on...

 

Now I need to remove everything after the model as that in turn is the vehicle specifications...

 

IE i want it to have a value of Vectra, Astra etc

 

I hope that's easy to understand??  If not ask away and I shall try my best to explain better...

 

so far I have...

 

$model=str_replace("ASTRA", "Astra", $model);
$model=str_replace("VECTRA", "Vectra", $model);

 

I have also tried...

$model=str_replace("%ASTRA%", "Astra", $model);
$model=str_replace("%VECTRA%", "Vectra", $model);

Any ideas ??

 

Many Thanks in advance :D

 

Link to comment
https://forums.phpfreaks.com/topic/159081-need-some-help-using-str_replace/
Share on other sites

if the the model is always one word you could aways find the index of the 1st space and get a substring of it.

 

<?php
$model = 'ASTRA CDTI LIFE';
$model = substr($model, 0, strpos($model, ' '));
$model = str_replace("ASTRA", "Astra", $model);

echo $model;
?>

 

would print out 'Astra'

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.