0

I have a URL, e.g: https://www.example.com/my-product-name-display/ex/BYADE3323/wgsi?nfh3420000ooo2323nfnf/.

From the above URL, I want to extract my-product-name-display if this URL contains it, if not, I want the string after /ex/{BYADE3323} as below URL does not contain my-product-name-display.

https://www.example.com/ex/BYADE3323/wgsi?nfh3420000ooo2323nfnf/

I have tried below code:

`$url_param = "https://www.example.com/ex/BYADE3323/wgsi?nfh3420000ooo2323nfnf/";`
or 
`$url_param = "https://www.example.com/my-product-name-display/ex/BYADE3323/wgsi?nfh3420000ooo2323nfnf/";`

    $e_product_title = explode('.com/', $url_param);
    if(isset($e_product_title)){
    $product_title = $e_product_title[1];
    //now explode the ex
    $get_asin = explode('/ex/',$product_title);
    $final_product_title = str_replace('-',' ',$get_asin[0]);
    $get_asin_final = explode('/', $get_asin[1]);
    $asin_v2 = $get_asin_final[0];
    }
    else{
        $get_asin = explode('/ex/',$url_param);
        print_r($get_asin);
    }
echo $final_product_title." ".$asin_v2;

Thanks in advance.

4
  • You want only product title from url like "BYADE3323" right? weather "my-product-name-display" string exist or not? Commented Apr 18, 2019 at 8:56
  • I want "my-product-name-display" if exists in url else want "BYADE3323" from url. Commented Apr 18, 2019 at 8:57
  • Is BYADE3323 a fixed string or it can have any value a that position? Commented Apr 18, 2019 at 8:59
  • "my-product-name-display" can be after .com/ or may not be avilable into URL. if my-product-name-display is not in url then after .com/ex/BYADE3323/ would be found into url, here BYADE3323 and my-product-name-display can not be same, it would be different for every new URL. Commented Apr 18, 2019 at 9:02

4 Answers 4

1

You can explode() the string,

Check if my-product-name-display and BYADE3323 is in the array.

If present, find out BYADE3323's index.

Add 1 to it and check if the next element is present.

<?php
$str = 'https://www.example.com/my-product-name-display/ex/BYADE3323/wgsi?nfh3420000ooo2323nfnf/';
$str = str_replace('://', '__', $str);
$arr = explode('/', $str);
$return = '';
if (in_array('my-product-name-display', $arr) && in_array('BYADE3323', $arr)) {
 $idx = array_search('BYADE3323', $arr);
 $idx2 = $idx + 1;
 if (! empty($idx) && ! empty($arr[$idx2])) {
  $idx += 1;
  $return = $arr[$idx2];
 }
}
echo $return;

EDIT:

As per comments from OP, following is the program for array of urls and array of search strings.

<?php
$searchStrings = [];
$searchStrings[] = ['my-product-name-display', 'BYADE3323'];
$searchStrings[] = ['your-product-name-display', 'BYADE4434'];

$urls = [];
$urls[] = 'https://www.example.com/my-product-name-display/ex/BYADE3323/wgsi?nfh3420000ooo2323nfnf/';
$urls[] = 'https://www.example.com/your-product-name-display/ex/BYADE4434/wgsi?nfh3420000ooo2323nfnf/';
$urls[] = 'https://www.example.com/their-product-name-display/ex/TEST343/wgsi?nfh3420000ooo2323nfnf/';
$urls[] = 'https://www.example.com/my-product-name-display/ex/ANASDF33/wgsi?nfh3420000ooo2323nfnf/';
$urls[] = 'https://www.example.com/my-product-name-display/ex/BYADE3323/wgsi?nfh3420000ooo2323nfnf/';

$return = [];
if (! empty($urls)) {
 foreach ($urls as $url) {
  if (! empty($searchStrings)) {
   foreach ($searchStrings as $searchString) {
    $str = implode('/ex/', $searchString);
    if (strpos($url, $str) !== false) {
     $arr = explode('/', $url);
     $idx = array_search('BYADE3323', $arr);
     $idx2 = $idx + 1;
     if (! empty($idx) && ! empty($arr[$idx2])) {
      $idx += 1;
      $return[] = $arr[$idx2];
     }
    }
   }
  }
 }
}
echo '<pre>';
print_r($return);
echo '</pre>';

Output:

Array
(
    [0] => wgsi?nfh3420000ooo2323nfnf
    [1] => wgsi?nfh3420000ooo2323nfnf
)
Sign up to request clarification or add additional context in comments.

5 Comments

but the my-product-name-display and "BYADE3323" is not static, it can be anything. i have approx 5 Lacks URLs. so how i extract them
You can add two arrays for them and need to find from array instead of string.
but how can i check if i don't know the next value of "my-product-name-display" and "BYADE3323", please help
is ex in the url fixed?
Yes it is fixed in url
0

Try this to fetch from URL values.

pass url to the function. You can extract it.

Here is the URL : https://www.example.com/my-product-name-display/ex/BYADE3323/wgsi?nfh3420000ooo2323nfnf/

So when u want only BYADE3323 this value. When you print $parts array, you can find every values after your Host name. Where your host name is https://www.example.com.

function GetStringAfterSecondSlashInURL($the_url)
    {
        $parts = explode("/",$the_url,3);

        if(isset($parts[2]))
            return $parts[2];
    }

Comments

0

Use parse_url() function this will help you definitely.

You can refer it from official PHP site: parse-url.

Comments

0

You can use strpos to identify weather 'my-product-name-display' is exist s in url or not and execute code accordingly.

strpos($url_param, 'my-product-name-display') !== false

Modified code:

function get_product_title($url_param) {
  $get_asin = explode('/ex/', $url_param);
  $get_asin_final = explode('/', $get_asin[1]);
  $asin_v2 = $get_asin_final[0];
  return $asin_v2;
}

$url_param = "https://www.example.com/ex/BYADE3323/wgsi?nfh3420000ooo2323nfnf/";
$url_param = "https://www.example.com/my-product-name-display/ex/BYADE3323/wgsi?nfh3420000ooo2323nfnf/";
$product_name = '';
if (strpos($url_param, 'my-product-name-display') !== false) {
  $e_product_title = explode('.com/', $url_param);
  if (isset($e_product_title)) {
    $product_title = $e_product_title[1];
    //now explode the ex
    $product_name = get_product_title($product_title);
  }
  echo "my product name display" . $product_name;
}
else {
  $product_name = get_product_title($url_param);
  echo $product_name;
}

3 Comments

here "my-product-name-display" is not fixed, that why it is creating issue. while extracting the data.
You mean, position of "my-product-name-display" is not fixed and you want to exact product name from it? Do you have some other sample urls?
Yes position of "my-product-name-display" is not fixed, and may be it not available into URL, sample url you have used in above code,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.