从URL中提取一些字符串

问题描述 投票:0回答:4

我有一个URL,例如:https://www.example.com/my-product-name-display/ex/BYADE3323/wgsi?nfh3420000ooo2323nfnf/

从上面的URL,我想提取my-product-name-display如果这个URL包含它,如果没有,我想在/ex/{BYADE3323}之后的字符串如下URL不包含my-product-name-display

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

我试过下面的代码:

`$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;

提前致谢。

php string
4个回答
1
投票

你可以explode()字符串,

检查my-product-name-displayBYADE3323是否在阵列中。

如果存在,找出BYADE3323的索引。

添加1并检查下一个元素是否存在。

<?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;

编辑:

根据OP的评论,以下是url数组和搜索字符串数组的程序。

<?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>';

输出:

Array
(
    [0] => wgsi?nfh3420000ooo2323nfnf
    [1] => wgsi?nfh3420000ooo2323nfnf
)

0
投票

尝试此操作以从URL值获取。

将url传递给函数。你可以提取它。

这是URL:https://www.example.com/my-product-name-display/ex/BYADE3323/wgsi?nfh3420000ooo2323nfnf/

所以,当你只想要BYADE3323这个值。当您打印$parts数组时,您可以在主机名后找到每个值。你的主机名是https://www.example.com

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

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

0
投票

使用parse_url()功能这肯定会帮助你。

您可以从官方PHP站点中引用它:parse-url


0
投票

你可以使用strpos识别天气'my-product-name-display'是否存在于url中并相应地执行代码。

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

修改后的代码

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.