繁体   English   中英

PHP从字符串生成HTML页面

[英]php generate html pages from string

我有这个脚本,可以从.xls列表中为公司拥有的每个商店位置生成静态html页面,然后该脚本根据关键字字符串对商店位置之后的url进行编码。

由于目前:

keyword_string = "key1 key2 key3 key4 key5";

function urlX($location) {
        return $this->xURL($location).'/'.urlencode($this->keyword_string).'.html';
    }

如何让它读取keyword_string的2个或什至3个变体并随机化html urlencode?

这将创建真正的随机链接。 如果您希望有一定数量的keyword_string集(即:keyword_string_1 =“ key1 key2 key3”,keyword_string_2 =“ key2 key3 key1”等),并选择一个随机集来附加到每个位置,则可以使用此方法以更简单的规模使用。 可能还有几种加快速度的方法。

$keyword_string = "key1 key2 key3 key4 key5";

function urlX($location) {
   global $keyword_string;
   $keyword_string = @explode(" ", $keyword_string);
   if(is_array($keyword_string)){
      $total = count($keyword_string);
      $random_keys = Array();
      for($i=0; $i<$total; $i++){
         $new = rand(0,$total-1);
         while(in_array($new,$random_keys)){
            $new = rand(0,$total-1);
         }
         array_push($random_keys, $new);
      }
   }
   $page = "";
   foreach($random_keys as $key){
      $page .= $keyword_string[$key] . " ";
   }

   return $location . "/" . urlencode(trim($page)) . ".html";

}

新增:

这是上面的代码,适用于一定数量的随机字符串。 您可以预加载其他字符串,并将它们添加到$keyword_string数组的末尾。

$keyword_string_1 = "key1 key2 key3 key4 key5";
$keyword_string_2 = "key2 key3 key4 key5 key1";
$keyword_string_3 = "key3 key4 key5 key1 key2";

/// ADD MORE HERE IF YOU LIKE
$keyword_string = Array($keyword_string_1, $keyword_string_2, $keyword_string_3); 

function urlX($location) {
   global $keyword_string;
   if(is_array($keyword_string)){
      $total = count($keyword_string);
      $random_keys = Array();
      $page = $keyword_string[rand(0,$total-1)];
   }
   return $location . "/" . urlencode(trim($page)) . ".html";
}

print urlX("http://www.google.com"); /// SIMPLE CALL TO NEW FUNCTION urlX()

新增:

$keyword_string_1 = "key1 key2 key3 key4 key5";
$keyword_string_2 = "key2 key3 key4 key5 key1";
$keyword_string_3 = "key3 key4 key5 key1 key2";

function urlX($location) {
global $keyword_string_1, $keyword_string_2, $keyword_string_3;    

    if($location == "http://www.url1.com"){ /// CHANGE THESE TO ACTUAL URLS
        return $location . "/" . urlencode($keyword_string_1) . ".html";
    }else if($location == "http://www.url2.com"){
        return $location . "/" . urlencode($keyword_string_2) . ".html";
    }else if($location == "http://www.url3.com"){
        return $location . "/" . urlencode($keyword_string_3) . ".html";
    }else{
        return false;
    }

}

print urlX("http://www.url1.com");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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