Solved

If string starts with XXX

Posted on 2014-12-29
6
178 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 110

Accepted Solution

by:
Ray Paseur earned 334 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 166 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
Technology Partners: 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 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 334 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 110

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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

749 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