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

 

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.