Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

If string starts with XXX

Posted on 2014-12-29
6
Medium Priority
?
194 Views
Last Modified: 2014-12-30
Hi

I am outputting two fields from my database... Brand & Title. I am outputting them as below...

<?php echo $row_rs_media_new['brand'] . " " . $row_rs_media_new['title']; ?>

Open in new window


My problem is on occasions in my data the title also shows the brand name so the brand name shows twice. The brand name is at the start of the title. How can I check if the start of the title has the brand name and if it does remove it along with the space after it?
0
Comment
Question by:petewinter
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1336 total points
ID: 40522977
See if this makes sense:
http://iconoun.com/demo/temp_petewinter.php

<?php // demo/temp_petewinter.php
error_reporting(E_ALL);

// MAKE IT EASIER TO READ THE OUTPUTS
echo '<pre>';

// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28588117.html
// REF http://php.net/manual/en/function.strpos.php
/* <?php echo $row_rs_media_new['brand'] . " " . $row_rs_media_new['title']; ?> */

// TEST CASE
$row_rs_media_new['brand'] = 'Chevrolet';
$row_rs_media_new['title'] = 'Chevrolet Corvette';

// SHOW THE INSTANT CASE
echo 'BEFORE: ';
echo PHP_EOL;
echo $row_rs_media_new['brand'] . " " . $row_rs_media_new['title'];
echo PHP_EOL;

// SHOW HOW TO DETECT AND REMOVE THE REDUNDANT INFORMATION WITH PHP STRPOS() FUNCTION
echo 'AFTER USING STRPOS(): ';
echo PHP_EOL;
if (strpos($row_rs_media_new['title'], $row_rs_media_new['brand']) === 0)
{
    echo $row_rs_media_new['title'];
}

Open in new window

0
 
LVL 9

Assisted Solution

by:Brian Tao
Brian Tao earned 664 total points
ID: 40523193
This should work:
<?php echo $row_rs_media_new['brand'] . " " . str_replace($row_rs_media_new['brand'] . " " , "", $row_rs_media_new['title']); ?>

Open in new window

0
 

Author Comment

by:petewinter
ID: 40524466
Thanks ray so I assume I need an else statement too. Like the below...

if (strpos($row_rs_media_new['title'], $row_rs_media_new['brand']) === 0)
{
    echo $row_rs_media_new['title'];
} else {
    echo $row_rs_media_new['brand'] . " " . $row_rs_media_new['title'];
}

Open in new window


taoyipai - Thanks, Your solution works too and is shorter.

Ray / taoyipai - Does it make any difference which option I use. Do they both do the same thing and load just as quickly? Any impacts I need to know about?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1336 total points
ID: 40524728
The overall strategy would be to see if the 'brand' is contained at the beginning of the 'title.'  These are PHP string variables.  PHP strings are documented here:
http://php.net/manual/en/language.types.string.php
http://php.net/manual/en/language.operators.string.php
http://php.net/manual/en/ref.strings.php

Your choice will have no observable effect on performance.  However your choice will depend on the details of the problem you want to solve.  Here are a couple of test cases.  You can install this script on your server exactly as written here and run it to see the results.

<?php // demo/temp_petewinter.php
error_reporting(E_ALL);

// MAKE IT EASIER TO READ THE OUTPUTS
echo '<pre>';

// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28588117.html
// REF http://php.net/manual/en/function.strpos.php
/* <?php echo $row_rs_media_new['brand'] . " " . $row_rs_media_new['title']; ?> */

// TEST CASE
$row_rs_media_new['brand'] = 'Chevrolet';
$row_rs_media_new['title'] = 'Chevrolet Corvette';

// SHOW THE INSTANT CASE
echo 'BEFORE: ';
echo PHP_EOL;
echo $row_rs_media_new['brand'] . " " . $row_rs_media_new['title'];
echo PHP_EOL;

// SHOW HOW TO DETECT AND REMOVE THE REDUNDANT INFORMATION WITH PHP STRPOS() FUNCTION
echo 'AFTER USING STRPOS(): ';
echo PHP_EOL;
if (strpos($row_rs_media_new['title'], $row_rs_media_new['brand']) === 0)
{
    echo $row_rs_media_new['title'];
}
?>


<?php

// TEST CASE
$row_rs_media_new['brand'] = 'Chevrolet';
$row_rs_media_new['title'] = 'Chevrolet Corvette';

echo PHP_EOL;
echo $row_rs_media_new['brand'] . " " . str_replace($row_rs_media_new['brand'] . " " , "", $row_rs_media_new['title']);

?>


<?php

// TEST CASE
$row_rs_media_new['brand'] = 'Corv';
$row_rs_media_new['title'] = 'Chevrolet Corvette';

echo PHP_EOL;
echo $row_rs_media_new['brand'] . " " . str_replace($row_rs_media_new['brand'] . " " , "", $row_rs_media_new['title']);

?>

Open in new window

Since we do not have your database we can't really give you a tested-and-working response without some guessing about what should be in the test data set.  If you want to follow the line of thinking that we use in devising test cases, this article may help.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html

If you're new to PHP and want some good learning resources, this article can help.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

And to your question about an else clause - yes you might want to have one of those.  It's worth experimenting with the code and your test data a little bit.  These kinds of things come together very quickly when you can produce some repeatable tests and run a few experiments.
0
 

Author Closing Comment

by:petewinter
ID: 40524770
Many thanks for the answer and info.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40524824
Thanks for the points!  I love it when great minds come together to solve problems.  Happy New Year to all, ~Ray
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question