From From From

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

33 Views Asked by At

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

<a href="nunm-press.com">

From

<a href="nunm-press.com"
to
<a href="http://nunm-press.com"

Suggestions?

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

2

There are 2 best solutions below

0
F. Gálvez On BEST ANSWER

you can use str_replace function

<?php
$string='<a href="nunm-press.com">';
$string_replace=str_replace('href="','href=\"http://',$string);
echo $string.' >> '.$string_replace;
?>
0
Muhammad Tariq On

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

$url = 'nunm-press.com';
$parsed_url = parse_url($url);
if (!isset($parsed_url['scheme'])) {
    $url = 'http://' . $url;
}
echo '<a href="' . $url . '">Link</a>';