PHP:如何在<a href="nunm-press.com">中插入缺失的HTTP?

huangapple go评论82阅读模式
英文:

PHP: how to insert HTTP in <a href="nunm-press.com"> that doesn't have any?

问题

需要将一个链接列表(10,000个)中没有HTTP的链接插入一个HTTP,例如:

  1. &lt;a href=&quot;nunm-press.com&quot;&gt;

  1. &lt;a href=&quot;http://nunm-press.com&quot;

有什么建议吗?

我搜索了“str_replace replace only if doesn't exist”,但没有找到任何结果。

英文:

I need to insert only a HTTP in a list of links (10,000) that doesn't have one, for exemple

  1. &lt;a href=&quot;nunm-press.com&quot;&gt;

From

  1. &lt;a href=&quot;nunm-press.com&quot;
  2. to
  3. &lt;a href=&quot;http://nunm-press.com&quot;

Suggestions?

I search for "str_replace replace only if doesn't exist", found nothing.

答案1

得分: 1

I can provide the translated code for you:

  1. 我不太理解你的问题,但如果你正在使用php并且想要设置httphttps,你可以尝试这样做。
  2. $url = 'nunm-press.com';
  3. $parsed_url = parse_url($url);
  4. if (!isset($parsed_url['scheme'])) {
  5. $url = 'http://' . $url;
  6. }
  7. echo '<a href="' . $url . '">Link</a>';

请注意,这只是给你提供了代码的中文翻译。如果你有任何其他问题或需要进一步的帮助,请随时告诉我。

英文:

I dont exactly understand your question but if you are using php and you want to set http or https you can try this.

  1. $url = &#39;nunm-press.com&#39;;
  2. $parsed_url = parse_url($url);
  3. if (!isset($parsed_url[&#39;scheme&#39;])) {
  4. $url = &#39;http://&#39; . $url;
  5. }
  6. echo &#39;&lt;a href=&quot;&#39; . $url . &#39;&quot;&gt;Link&lt;/a&gt;&#39;;

答案2

得分: 0

你可以使用str_replace函数。

  1. <?php
  2. $string = '<a href="nunm-press.com">';
  3. $string_replace = str_replace('href="', 'href="http://', $string);
  4. echo $string.' >> '.$string_replace;
  5. ?>
英文:

you can use str_replace function

  1. &lt;?php
  2. $string=&#39;&lt;a href=&quot;nunm-press.com&quot;&gt;&#39;;
  3. $string_replace=str_replace(&#39;href=&quot;&#39;,&#39;href=\&quot;http://&#39;,$string);
  4. echo $string.&#39; &gt;&gt; &#39;.$string_replace;
  5. ?&gt;

huangapple
  • 本文由 发表于 2023年5月7日 04:15:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190936.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定