firefly
firefly

Reputation: 906

How can I figure out in array each element contains a certain word?

I'm trying to add word in each array elements. But one of the element has that word. That's I want to pass that, and add the word other elements.

Here is my array.

string(56) "//sumai.tokyu-land.co.jp/bukken/detail/index/id/K4810000"
[1]=>
string(56) "//sumai.tokyu-land.co.jp/bukken/detail/index/id/K4780000"
[2]=>
string(56) "//sumai.tokyu-land.co.jp/bukken/detail/index/id/K4720000"
[3]=>
string(56) "//sumai.tokyu-land.co.jp/bukken/detail/index/id/K4760000"
[4]=>
string(62) "https://sumai.tokyu-land.co.jp/bukken/detail/index/id/K4770000"
[5]=>
string(56) "//sumai.tokyu-land.co.jp/bukken/detail/index/id/K4740000"

The forth one has https: but the other has not. I want to add all http: all of them.

$http = 'https:';

foreach($iframeLink as $value)
{
    if (!$value contains 'http')
        $iframe[] = $http.$value;
}

Contains is not working. It's simple thing but I couldn't figure it out. Any help? Thank You!

Upvotes: 2

Views: 119

Answers (4)

Gufran Hasan
Gufran Hasan

Reputation: 9373

You can parse URL and check it scheme

$http = 'https:';
foreach($iframeLink as $value)
{
    $parsedUrl = parse_url($value);
   if ($parsedUrl['scheme']!='http') {
     $iframe[] = $http.$value;
   }
}

Upvotes: 1

Madhur Bhaiya
Madhur Bhaiya

Reputation: 28834

You can use stripos() function. It finds the first occurrence of a given substring within the string. So, if stripos($value,'https:') returns false; it basically implies that the string $value does not contain 'https:'

foreach($iframeLink as $value)
{
    if ( stripos($value,'https:') === false )
        $iframe[] = $http.$value;
}

Now, there is a chance 'https: may exist at other positions in the string. And, you need to prepend 'https: only when it is not at the beginning of the given string. In that case, you can try the following instead:

foreach($iframeLink as $value)
{
    if ( stripos($value,'https:') > 0 )
        $iframe[] = $http.$value;
}

Upvotes: 3

Mohammad
Mohammad

Reputation: 21489

Use substr() to selecting special part of string and check that is https or not.

$http = 'https:';
foreach($iframeLink as $key=>$value){
    if (substr($value, 0, 6) != $http)
        $iframe[] = $http.$value;
}

Check result in demo

Upvotes: 2

Tharaka Dilshan
Tharaka Dilshan

Reputation: 4499

In laravel there is a helper function str_contains()

foreach($iframeLink as $value)
{
    if (str_contains($value, 'https:')) $iframe[] = $http.$value;
}

Upvotes: 2

Related Questions