2015-02-06 57 views
-5

我有一個段落如何在PHP中替換段落中的單詞?

"hi boss how is your health. <p><div> my office is big Perl </div></p>" 

我想單獨刪除<div></div>標籤,使更新後的字符串像

"hi boss how is your health. <p>my office is big Perl</p>". 

我應該怎麼辦?

+0

你想要什麼,正確解釋 – user3647254 2015-02-06 10:34:32

+0

這兩個字符串是相同的,但如果你想刪除空間使用''rtrim()''' – JammyDodger231 2015-02-06 10:34:52

+2

你有這個意思嗎?:[strip_tags](http:// p hp.net/strip_tags) – 2015-02-06 10:35:15

回答

0

PHP路,使用str_replace如下:

<?PHP 
$string = "hi boss how is your health. <p><div>my office is big Perl</div></p>"; 

$string=str_replace('<div>', '', $string); 
$string=str_replace('</div>', '', $string); 

echo $string; 
?> 

簡單的方法,訪問任何在線文字處理工具的網站,而不需要一次編輯你的整個文本段落任何PHP知識EX:Text Manipulation Tools

+0

但它不打印鏈接//打印「嗨,老闆,你的健康。

我的辦公室很大Perl

「。回聲像這樣//打印」嗨,老闆,你的健康狀況如何。我的辦公室是大Perl「 – 2015-02-06 11:15:12

+0

我編輯了代碼,請注意,它打印'嗨老闆你的健康狀況如何。

我的辦公室是大Perl

'如果你只是作爲它的HTML代碼查看源代碼,請參閱圖像http ://i.cubeupload.com/5eFjhy.png – 2015-02-06 11:36:03

+0

我收到了謝謝:) – 2015-02-06 12:26:52

0

您正在尋找的是內置於PHP中的strip tags功能:http://php.net/strip_tags它從輸入字符串中去除html標籤,例如

$text = "hi boss how is your health. <div> my office is big Perl </div>"; 
echo strip_tags($text); 
//Will echo "hi boss how is your health. my office is big Perl" 
+0

:沒有,如果我的文本包含$ text =「hi boss你的健康狀況如何

my office is big Perl

」;我想刪除
單獨 – 2015-02-06 10:39:28

+0

如果哪些文本包含文本變量? – JammyDodger231 2015-02-06 10:40:40

1

只需要使用用strip_tags函數在PHP中刪除。使用下面的代碼

<?php 
$string = "hi boss how is your health. <div> my office is big Perl </div>"; 
echo strip_tags($string,"<p>"); // Prints "hi boss how is your health. my office is big Perl" 
?> 

如果需要,您可以允許一些或多個標籤。請閱讀完整的參考這裏 http://php.net/strip_tags

希望這有助於你的PHP

+0

現在我編輯了我的問題。 u能回答 – 2015-02-06 10:48:30

+0

檢查我的答案,如果它的工作原理 – user3647254 2015-02-06 10:52:33

+0

如果$字符串=「你好老闆怎麼您的健康

my office is big Perl

。」我應該只更換
結果「嗨,老闆怎麼是你的健康。

我的辦公室是大的Perl

「 – 2015-02-06 10:56:37

1

使用strip_tags($string);功能。

更多參考:http://php.net/strip_tags

+0

:我editd問題友好地回答 – 2015-02-06 10:46:43

1

如果妳只想刪除div標籤,然後使用str_replace函數如下─一樣

$string = "hi boss how is your health. <p><div>my office is big Perl</div></p>"; 
$string = str_replace('<div>' , '' , $string); 
$string = str_replace('</div>' , '' , $string); 

和如果u要刪除所有HTML標記,然後用用strip_tags按其他成員給出了建議。

+0

感謝和好 – 2015-02-06 11:01:52

+0

但不工作。檢查我編輯的問題 – 2015-02-06 11:11:20

+0

,因爲我錯過了我的字符串p標記第一次..im糾正我的代碼 – TECHNOMAN 2015-02-06 11:44:53

相關問題