PHP 'Delete all between' specific words

php download
php code
php tutorial
php stands for
php insurance
php wiki
php abbreviation
php currency

I have a list of items. Each item has its description (the description is different for each item, but its structure remains the same) that looks kind of like that:

[description] => Flat sandal <br />Blush<br />Laminated leather<br />Intertwining straps<br />Low heel: 0.5cm<br />

        Product code: 5276870PS006703 <br /> Made in: Italy<br />Composition: 100%Calfskin

I need to delete the part "Product code: (random numbers and letters)" for every item description. I thought about using string_replace, but it would only work for replacing the words 'Product code', but not the numbers and letters, because they are different for every item. I also tried:

$description = delete_all_between("Product code:", "<br />", $description);

But it didn't work. Not sure what else I can try.

Thank you

Use the preg_replace()-function

$result = preg_replace('/product\s+code[^>]*\>/is', '', $input);

Regular Expression

look for    "product"
followed by \s+ (one or more spaces, tabs,...)
followed by "code"
followed by [^>]* (an unspecified amount of charakters that are not ">")
followed by \> an ">" (\ is es for escaping)

Flag

i = ignore upper/lowercase
s = search multiple lines

What is PHP? - Manual, The PHP Hypertext Preprocessor (PHP) is a programming language that allows web developers to create dynamic content that interacts with databases. PHP is  PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft's ASP. PHP 7 is the latest stable release. Start learning PHP now »

You can adapt the following code:

$description = 'Flat sandal <br />Blush<br />Laminated leather<br />Intertwining straps<br />Low heel: 0.5cm<br />

        Product code: 5276870PS006703 <br /> Made in: Italy<br />Composition: 100%Calfskin';
$pattern = '/Product code:\s*\w*\s*<br />/';
$replacement = '';
echo preg_replace($pattern, $replacement, $description);

final output:

Flat sandal 
Blush
Laminated leather
Intertwining straps
Low heel: 0.5cm
Made in: Italy
Composition: 100%Calfskin

Explanations:

preg_replace is a php function that will replace in an input string a specific pattern defined by a regex by a replacement

The regex used Product code:\s*\w*\s*<br /> demo will match strings that starts by Product code: followed by some space chars, followed by some word chars, more space chars before ending by the html <br /> (Regex quicksheet).

PHP Tutorial, An easy-to-read, quick reference for PHP best practices, accepted coding standards, and links to authoritative PHP tutorials around the Web. PHP is a popular general-purpose scripting language that is especially suited to web development. It was originally created by Rasmus Lerdorf in 1994; the PHP reference implementation is now produced by The PHP Group. PHP originally stood for Personal Home Page, but it now stands for the recursive initialism PHP: Hypertext Preprocessor.

You need to look at preg_replace, which uses regex and give you a lot of power to target whatever you after.

Something like

$string = 'Flat sandal <br />Blush<br />Laminated     leather<br/>Intertwining straps<br />Low heel: 0.5cm<br />Product code: 5276870PS006703 <br /> Made in: Italy<br />Composition:';
$pattern = '/Product code: (w+) /i';
$replacement = '';
echo preg_replace($pattern, $replacement, $string);

Hope that helps

PHP, Learn the fundamentals of PHP, one of the most popular languages of modern web development. PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. Nice, but what does that mean?

PHP Tutorial, PHP is a general-purpose scripting language widely used as a server-side language for creating dynamic web pages. Though its reputation is mixed, PHP is still  PHP is a widely-used, open source scripting language PHP scripts are executed on the server PHP is free to download and use PHP is an amazing and popular language!

PHP: The Right Way, PHP is a powerful scripting language that fits gracefully into HTML and puts the tools for creating dynamic websites in the hands of the people  PHP is a server side scripting language that is embedded in HTML. It is used to manage dynamic content, databases, session tracking, even build entire e-commerce sites. It is integrated with a number of popular databases, including MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL Server.

Learn PHP, PHP is a server side scripting language. that is used to develop Static websites or Dynamic websites or Web applications. PHP stands for  PHP Agency is a national, financial services company started in 2009 with humble origins and a grand vision. Our mission since day one is to change an elitist financial services industry and in so doing, provide EVERYONE access to the best insurance products offered.

Comments
  • Use a regular expression replacement.