简体   繁体   中英

Removing first part of string?

Trying to edit urls in array.

[0] => https://www.proud-web.jp/mansion/b115110/https://www.proud-web.jp/module/structure/outline/BukkenOutline.xphp?code_no=011244
[1] => https://www.proud-web.jp/mansion/p-ebisuminami88/https://www.proud-web.jp/module/structure/outline/BukkenOutline.xphp?code_no=011205

As you see urls are like this. trying to remove first url and contain the second.

expected result is like:

https://www.proud-web.jp/module/structure/outline/BukkenOutline.xphp?code_no=011244
https://www.proud-web.jp/module/structure/outline/BukkenOutline.xphp?code_no=011205

what I tried is right below. In that way I can only remove the second. But how can I fix this code to remove first url in the string not the second.

$result = [];
foreach($setLinks as $key) {
   array_push($result, current(explode("/h", $key)));
}

You can use foreach followed by explode to get split the string wrt /https . Below is the code:

$array = ['https://www.proud-web.jp/mansion/b115110/https://www.proud-web.jp/module/structure/outline/BukkenOutline.xphp?code_no=011244','https://www.proud-web.jp/mansion/p-ebisuminami88/https://www.proud-web.jp/module/structure/outline/BukkenOutline.xphp?code_no=011205'];
$result = [];
foreach($array as $arr){
  $getUrl = explode('/https', $arr);
  array_push($result, 'https' . $getUrl[1]);
}

print_r($result);

This is what I would do:

$urls = [
    'https://www.proud-web.jp/mansion/b115110/https://www.proud-web.jp/module/structure/outline/BukkenOutline.xphp?code_no=011244',
    'https://www.proud-web.jp/mansion/p-ebisuminami88/https://www.proud-web.jp/module/structure/outline/BukkenOutline.xphp?code_no=011205'
];

foreach($urls as &$url){
    $url = 'http'.preg_split('/^.+?\/http/', $url, 2, PREG_SPLIT_NO_EMPTY)[0];
}

print_r($urls);

Output

Array
(
    [0] => https://www.proud-web.jp/module/structure/outline/BukkenOutline.xphp?code_no=011244
    [1] => https://www.proud-web.jp/module/structure/outline/BukkenOutline.xphp?code_no=011205
)

Sandbox

I set it up so that it would handle both HTTP and HTTPS

try this

unset($setLinks[0]);
foreach($setLinks as $key) {
   echo $key;
}

You could use preg_replace to remove the leading text:

foreach ($setLinks as &$value) {
    $value = preg_replace('#^.+(https?://.*)$#', '$1', $value);
}
print_r($setLinks);

Output:

Array (
    [0] => https://www.proud-web.jp/module/structure/outline/BukkenOutline.xphp?code_no=011244
    [1] => https://www.proud-web.jp/module/structure/outline/BukkenOutline.xphp?code_no=011205 
)

Demo on 3v4l.org

I would separate the task in 3 subtasks.

  • First one being to capture the protocol using regex in example (the protocol of that url could be https, http, ftp ...)
  • Then, capture the url itself, splitting the string using :// as delimiter
  • Finally, rebuild protocol . "://" . url protocol . "://" . url

In example :

<?php
$array =
[
    'https://www.proud-web.jp/mansion/b115110/https://www.proud-web.jp/module/structure/outline/BukkenOutline.xphp?code_no=011244',
    'https://www.proud-web.jp/mansion/p-ebisuminami88/https://www.proud-web.jp/module/structure/outline/BukkenOutline.xphp?code_no=011205',
    'http://www.example.com/home/http://something',
    'http://www.example.com/https/ftp://something',
    'https://nothing.to.capture'
];

$result = array();
/*
 * matches a slash -> \/
 * followed by letters -> ([a-z]*)
 * followed by :// -> :\/\/
 * and capture the letters -> (the parenthesis)
 * it can match, in example : something/mycustomprotocol://somethingelse
 */
$pattern = "/\/([a-z]*):\/\//i";
foreach($array as $item) {
    preg_match_all($pattern, $item, $matches);
    if (count($matches) > 0)
    {
        $urls = explode("://", $item, 3);
        if (count($urls) > 2)
        {

            $protocol = $matches[1][0];
            $result[] = $protocol . "://" . $urls[2];

        }
    }
}
var_dump($result);

Output

array(4) {
  [0]=>
  string(83) "https://www.proud-web.jp/module/structure/outline/BukkenOutline.xphp?code_no=011244"
  [1]=>
  string(83) "https://www.proud-web.jp/module/structure/outline/BukkenOutline.xphp?code_no=011205"
  [2]=>
  string(16) "http://something"
  [3]=>
  string(15) "ftp://something"
}

i miss understand your question .kindly try it

<?php $quest = array("https://www.proud-web.jp/mansion/b115110/https://www.proud-web.jp/module/structure/outline/BukkenOutline.xphp?code_no=011244",
"https://www.proud-web.jp/mansion/p-ebisuminami88/https://www.proud-web.jp/module/structure/outline/BukkenOutline.xphp?code_no=011205");

foreach($quest as $q )
{
  $allquest = explode("/https",$q);
  echo "https".$allquest[1];

}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM