0

I'm currenty trying to format some parts of a Brandname, but I'm stuck with getting the part that I need to format, example:

BrandTest®
BrandBottle®
BrandJuice®

I would like to have the parts between Brand and the ®.

I currently tried something like: /(?=(Brand))+(.*)+(®)/

But I'm getting everything except the part in the middle.

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125

3 Answers3

1

You can change your regex to use this:

Brand(.*?)®

Working demo

Php code

$re = "/Brand(.*?)®/"; 
$str = "BrandTest® BrandBottle® BrandJuice®"; 

preg_match_all($re, $str, $matches);
Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
0

Maybe like this:

<?php
$brands = 'BrandTest® BrandBottle® BrandJuice®';
$brands = explode('Brand', $brands);
//you will get each brand in an array as:
//"Test®", "Bottle®", "Juice®"
?>

If you don't want the ® then this might help https://stackoverflow.com/a/9826656/4977144

Community
  • 1
  • 1
xYuri
  • 369
  • 2
  • 17
0

Roll it up into a method and return your result:

function getQueryPiece($queryString) {
    preg_match('/\?\=Brand(.*)®/',$queryString,$matches);
    return $matches[1];
}

getQueryPiece("?=BrandBottle®"); // string(6) Bottle  
getQueryPiece("?=BrandTest®"); // string(4) Test  
getQueryPiece("?=BrandJuice®"); // string(5) Juice  

This defines only 1 capturing group (the piece of the string between ?=Brand and ®). If you need to capture the other parts as well, just wrap each part in parens: '/(\?\=Brand)(.*)(®)/' However this will alter the position of that piece in the $matches array. It would be position 2.

I believe the initial problem in your pattern is the result of using '?' unescaped. Question marks have special meanings in regular expressions. Here's a good write up regarding that: Regex question mark

Community
  • 1
  • 1
Robert Wade
  • 4,918
  • 1
  • 16
  • 35