2016-04-14 35 views
-2

Sadece düzeltmek için yardımcı olabilecek basit bir hata yaptığımı umuyorum. Ne yazık ki PHP ile çok deneyimli değilim.Çalışmak için preg_replace alınamıyor

Bir dize üzerinde iki normal ifadeyi çalıştırmaya çalışıyorum.

Birincisi komut dosyasını ve iframe'i bulmayı denediğinizde ve bunun etrafında bir boşluk bıraktığınızı göreceksiniz. İkincisi, "//" URL'lerini HTTP protokolüyle değiştirmeyi deniyor - bunun, aşağıda belirttiğim str_replace olarak yapılabileceğini anlıyorum. Str_replace'ın bu işlevin çağrılmadığı komik bir iş olmadığını ve iyi çalıştığını garanti etmek için çalışıyordum. Bazı nedenlerden dolayı preg_replace temel olarak yok sayılıyor ve dize değişmiyor.

Burada bariz bir şey eksik miyim?

Birkaç çevrimiçi preg_replace aracını denedim ve doğru görünüyorlar.

function cleanseSpringboardEmbed($content) 
{ 
    // run regex over the content to clean up the embed code from springboard and make compatible with IA. 
    $patternWrapper = '/<script src="\/\/www.springboardplatform\.com\/js\/overlay"><\/script><iframe(.*)<\/iframe>/'; 

    $patternProtocol = '/<iframe src="\/\/cms.springboardplatform.com/'; 

    $holder = $content; 

    $replacementWrapper = '<figure class="op-interactive">' . '$0' . '</figure>'; 
    $replacementProtocol = '<iframe src="http://cms.springboardplatform.com'; 

    //$holder = str_replace("//cms.springboardplatform.com","http://cms.springboardplatform.com", $holder); 
    //$holder = str_replace("//www.springboardplatform.com","http://www.springboardplatform.com", $holder); 

    preg_replace($patternWrapper, $replacementWrapper, $holder); 
    preg_replace($patternProtocol, $replacementProtocol, $holder); 
    return $holder; 
} 

İşte sen preg_replace gerçekleştirdikten sonra modifiye tutucu değeri atamak için unuttum bazı girdi

<p>test<br /> 
<script src="//www.springboardplatform.com/js/overlay"></script><iframe id="crzy003_1621795" src="//cms.springboardplatform.com/embed_iframe/5365/video/1621795/crzy003/craziestsportsfights.com/10" width="600" height="400" frameborder="0" scrolling="no"></iframe><br /> 
test</p> 

cevap

1

bir örnek. konu parametresi bir dizi, veya başka bir dize ise manuel sayfa üzerinde

preg_replace() göre bir dizi döndürür.

Eşleşmeler bulunursa, yeni konu döndürülür, aksi halde konusu değiştirilmez veya bir hata oluştuğunda NULL gönderilir.

Yani bunun için kodunuzu değiştirmeniz gerekir:

<?php 

function cleanseSpringboardEmbed($content) 
{ 
    // run regex over the content to clean up the embed code from springboard and make compatible with IA. 
    $patternWrapper = '/<script src="\/\/www.springboardplatform\.com\/js\/overlay"><\/script><iframe(.*)<\/iframe>/'; 

    $patternProtocol = '/<iframe src="\/\/cms.springboardplatform.com/'; 

    $holder = $content; 

    $replacementWrapper = '<figure class="op-interactive">' . '$0' . '</figure>'; 
    $replacementProtocol = '<iframe src="http://cms.springboardplatform.com'; 

    //$holder = str_replace("//cms.springboardplatform.com","http://cms.springboardplatform.com", $holder); 
    //$holder = str_replace("//www.springboardplatform.com","http://www.springboardplatform.com", $holder); 

    $holder = preg_replace($patternWrapper, $replacementWrapper, $holder); 
    $holder = preg_replace($patternProtocol, $replacementProtocol, $holder); 
    return $holder; 
} 
+1

çok teşekkür ederim! –