Solved

If string starts with XXX

Posted on 2014-12-29
6
169 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
  • 3
  • 2
6 Comments
 
LVL 108

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 108

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

939 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now