?
Solved

If string starts with XXX

Posted on 2014-12-29
6
Medium Priority
?
188 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
Independent Software Vendors: 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

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

801 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