【问题标题】:MYSQL Replace string between two known stringsMYSQL 替换两个已知字符串之间的字符串
【发布时间】:2014-05-18 19:57:53
【问题描述】:

编辑 1:我正在尝试使用下面的查询来获取,但问题是结束 div 没有在开始点之后发生,即

select ID, post_title, substr(post_content,instr(post_content,'<div style="position:absolute') + 0, instr(post_content,'</div>')) as temp from wp_posts where post_content LIKE '%mySpamFilter Text%' LIMIT 10

我的一个博客受到了垃圾邮件发送者的攻击,我有大约 100 多个帖子,其中包含垃圾邮件发送者融合的不需要的链接或消息。我想知道如何替换两个字符串之间的字符串。

Start string = '<div style="position:absolute;'
End String = '</div>'  (This End div should be the first end div after the above Start string)

我尝试了堆栈溢出时可用的所有选项,但到目前为止没有运气。请建议继续前进的最佳方法是什么。下面是来自 wp_posts 表的帖子之一的 post_content 列的内容 您可以在 Paneer 一词之后看到 div 类之间有很多垃圾邮件。我想从我的数据库中摆脱它。我不知道我是如何成为这次攻击的受害者的,但现在我只想删除这些。

 <p>Hey Foodies,</p>
<p>Biryani, the pride of Hyderabad, is definitely one dish which everyone knows to cook in their very own ways and styles. Biryani is one such thing which totally distinguishes itself with the plain rice we eat daily and the regular pulao. Biryani is the BIRYANI. So, lets look into the detailed procedure of the much loved Hyderabadi delicacy, the vegetarian version. Oh yes, this is the Hyderabadi Dum Veg Biryani!</p>
<p>This recipe would take 380-400 grams of uncooked Basmati rice which would serve 6.</p>
<div class="easyrecipe">
<link itemprop="image" href="http://mywebsite.in/wp-content/uploads/2013/09/Biryani-300x225.jpg">
<div class="item ERName">Hyderabadi Veg Dum Biryani</div>
<div class="ERClear"></div>
<div class="ERHead"><span class="xlate">Recipe Type</span>: <span class="type">Main Course</span></div>
<div class="ERHead">Cuisine: <span class="cuisine">Indian</span></div>
<div class="ERHead">Prep time: <time itemprop="prepTime" datetime="PT30M">30 mins</time></div>
<div class="ERHead">Cook time: <time itemprop="cookTime" datetime="PT1H15M">1 hour 15 mins</time></div>
<div class="ERHead">Total time: <time itemprop="totalTime" datetime="PT1H45M">1 hour 45 mins</time></div>
<div class="ERHead">Serves: <span class="yield">6</span></div>
<div class="ERIngredients">
<div class="ERIngredientsHeader">Ingredients</div>
<ul class="ingredients">
<li class="ingredient">For Vegetables: Carrot - 1 medium</li>
<li class="ingredient">Potatoes - 1 medium</li>
<li class="ingredient">Paneer ***<div style="position:absolute; left:-3905px; top:-3984px;">Gloves fast it <a href="http://www.goprorestoration.com/natural-viagra-foods">natural viagra foods</a> recently! Like as <a href="http://www.backrentals.com/shap/daily-cialis.html">daily cialis</a> the purchase 1 pad <a href="http://www.vermontvocals.org/buy-online-cialis.php">generic viagra cheap</a> and matte easy <a href="http://www.teddyromano.com/blue-pills/">blue pills</a> me many. Content but <a href="http://augustasapartments.com/qhio/10-mg-cialis">10 mg cialis</a> the as the helps Amazon <a href="http://www.mordellgardens.com/saha/cheap-viagra-canada.html">http://www.mordellgardens.com/saha/cheap-viagra-canada.html</a> . And baths etc would. I've <a href="http://www.teddyromano.com/brand-cialis/">brand cialis</a> Appears dollars hands still <a href="http://www.backrentals.com/shap/generic-cialis-europe.html">shop</a> October a smell <a href="http://www.hilobereans.com/buy-real-viagra/">http://www.hilobereans.com/buy-real-viagra/</a> in have. Of better <a href="http://www.creativetours-morocco.com/fers/herbal-viagra-australia.html">cheap generic cialis</a> it smelled to and <a rel="nofollow" href="http://www.goprorestoration.com/best-price-viagra">best price viagra</a> t customer anything been <a href="http://www.mordellgardens.com/saha/viagra-online-reviews.html">viagra online reviews</a> 5 supply time <a href="http://www.hilobereans.com/viagra-canada-pharmacy/">"here"</a> the product what <a href="http://www.vermontvocals.org/ed-supplements.php">buy levitra</a> biological by cream!</div>***  - 1/2 cup</li>
<li class="ingredient">Cauliflower - 1/2 cup</li>
<li class="ingredient">Green peas - 1 tbsp.</li>
<li class="ingredient">Yogurt - 1/2 cup</li>
<li class="ingredient">Mint - 1 tbsp.</li>
<li class="ingredient">Coriander - 1 tbsp.</li>
<li class="ingredient">Ginger-Garlic paste - 1 tsp.</li>
<li class="ingredient">Green chili - 4 to 5</li>
<li class="ingredient">Oil - 2 tbsp</li>
<li class="ingredient">Salt - As per taste</li>
<li class="ingredient">Biryani Masala - 1 and 1/2 tbsp.</li>
<li class="ingredient">Red Chili Powder - 1/2 tsp.</li>
<li class="ingredient">Garam Masala Powder - 1/4 tsp.</li>
<li class="ingredient">Whole Garam Masala - Custom or readymade</li>
<li class="ingredient">For Rice: Basmati Rice(soaked for 1 hour) - 380 gms</li>
<li class="ingredient">Water - 6-8 cups</li>
<li class="ingredient">Oil - 1-2 Tbsp.</li>
<li class="ingredient">Whole Garam masala - Custom or readymade</li>
<li class="ingredient">Salt - 3 Tsp.</li>
<li class="ingredient">For Dum(Bringing together): Yogurt - 1/2 cup</li>
<li class="ingredient">Saffron strands - dipped in milk, a few</li>
<li class="ingredient">Chopped Nuts - As desired</li>
<li class="ingredient">Mint and Coriander - two handfuls</li>
<li class="ingredient">Fried onions - two handfuls</li>
<li class="ingredient">Green chili - 4-5 slits</li>
<li class="ingredient">Water - 1/4 cup</li>
</ul>
</div>
<div class="ERInstructions">
<div class="ERInstructionsHeader">Instructions</div>
<div class="instructions">
<ol>
<li class="instruction">For Veggies: In a kadhai add oil. Now add whole garam masala, ginger-garlic paste. Saute for a minute. Now add carrot, potatoes, cauliflower, peas and salt. Saute until they are lightly cooked.</li>
<li class="instruction">After the veggies are a bit cooked, add in biryani masala, red chili powder, garam masala powder and gradually add yogurt stirring well to prevent curdling of curd.</li>
<li class="instruction">Now add mint, coriander, paneer. Cook until the moisture gets dried out which was caused due to adding yogurt. This would take approx 5 minutes. Veggies should not be cooked completely as we will dum it later on. It should 70% cooked.</li>
<li class="instruction">After the moisture is dried, add some fried onions and transfer to a bowl.</li>
<li class="instruction">For Rice: Boil water. Add oil, whole garam masala, salt. Boil until roaring boil. Add the soaked rice. In about 3-4 minutes in high flame rice will be 70% done. Test by eating some if it has a bite but its tastes like cooked or press between ur fingers. If breaks into 2 parts its ready. Strain it and spread on a big plate.</li>
<li class="instruction">For Dum: Take a cooker, add half of the vegetables, yogurt, green chilli, a pinch of salt and some nuts. Let it heat. Now add a handful of mint coriander. Now top it off with a good layer of rice.</li>
<li class="instruction">Next add the remaining vegetables, remaining mint-coriander(leave 1 tsp. for top), fried onions and the rice. Spread the rice and add the saffron mixture with a bit of mint-coriander, fried onion.</li>
<li class="instruction">Now sprinkle 1/4 cup of water over it, Cover and cook for 2 minutes in lowest flame.</li>
<li class="instruction">Transfer the cooker to a big pot with some water. This water bath prevents the dum to get burnt.. In about 30-35 minutes the Biryani is ready to make you dig into it.</li>
</ol>
</div>
</div>
<div class="ERNutrition"></div>
<div>
<div class="ERNotesHeader">Notes</div>
<div class="ERNotes">Cut vegetables in a uniform size.[br]Veggies are less, but the rice is used more for Biryanis.[br]You could use raw chopped nuts or saute them in oil and add.[br]Make sure to soak rice for atleast 1 hour.</div>
</div>
<div class="endeasyrecipe" style="display: none;">3.2.1255</div>
</div>
<p>&nbsp;</p>
<p><a href="http://mywebsite.in.cp-25.webhostbox.net/wp-content/uploads/2013/09/Biryani-2.jpg"><img class=" wp-image-411 alignright" alt="Biryani-2" src="http://mywebsite.in.cp-25.webhostbox.net/wp-content/uploads/2013/09/Biryani-2.jpg" width="189" height="142"></a></p>
<p>Recipe Ingredients are lengthy.. But it looks only onscreen.. Once you start preparing them they does not seem long or more.. Basic Ingredients, Easy Recipe and Traditional Hyderabadi Taste.. Dont forget to try it, I am sure you'll love it and make it again and again!!!</p>
<p><a href="http://www.youtube.com/mywebsite" target="_blank">Do Subscribe on YouTube</a></p>
<p><a href="http://www.fb.com/mywebsite" target="_blank">Like us on FB</a></p>
<p>&nbsp;</p>
<p>Video Recipe:</p>
<p><iframe src="//www.youtube.com/embed/zf0_AJfp5AA" height="500" width="835" allowfullscreen="" frameborder="0"></iframe></p>

【问题讨论】:

  • 你应该清理你的输入。一种可能的方法是拒绝包含 html 标记的成分的任何输入。你也可以限制配料的大小。如果您只是想要链接不可点击,那么您可以使用 htmlspecialchars() 来输出成分。
  • 这是一种预防措施吗..?,我不明白我应该做什么,我对你使用的术语很陌生
  • 是的,这是一种预防措施。但是通过相同的方法,您应该能够过滤垃圾邮件。我假设每种成分都等于表中某一列的一行中的值。我将首先过滤那些包含文本&lt;a 和文本&lt;/a 的行。这可以给你一个很好的起点
  • 只是为了确保我们都在同一页面上 1. 我知道其中包含垃圾邮件 cmets 的帖子 2. 我不能像我一样只
  • 您的示例数据未显示任何 html 标记。所以我以为没有。如果您想通过 substr 操作删除它,我建议在清理之前备份表:-)

标签: mysql sql phpmyadmin


【解决方案1】:

您可以使用以下查询进行此替换:

UPDATE Customer SET address = REPLACE(address, substring_index(substring_index(address, 'prefixString', -1), 'postfixString', 1), '') WHERE address IS NOT NULL;

例如:

地址:黑助推鹿街

booster 替换为空字符串。

UPDATE Customer SET address = REPLACE(address, substring_index(substring_index(address, 'Black', -1), 'deer', 1), ' ') WHERE address IS NOT NULL;

【讨论】:

    【解决方案2】:

    答案是以下变体:

    update wp_posts
    set post_content = CONCAT(LEFT(post_content, LOCATE('div style=', post_content)-1),  
                       SUBSTRING(post_content, LOCATE('</div>', post_content)+7))
    where LOCATE('div style=', post_content) > 0
    

    将“div 样式”更改为任何有效的开头。

    【讨论】:

      【解决方案3】:

      您的查询已关闭,但您需要使用 LOCATE 以及 INSTR 来查找嵌入字符串的结尾:

      select 
        ID, 
        post_title, 
        substr(post_content,
         instr(post_content,'<div style="position:absolute'), 
         LOCATE('</div>',post_content,instr(post_content,'<div style="position:absolute')+30)-instr(post_content,'<div style="position:absolute')
         +6) as temp 
      from wp_posts 
      where post_content LIKE '%<div style="position:absolute%' 
      LIMIT 10
      

      在此处查看示例:http://sqlfiddle.com/#!2/d0cf2f/13

      【讨论】:

      • SubStr 返回空 :(
      • 已修复 - 我忘记了 INSTR & LOCATE 中的参数顺序颠倒了。对不起。
      • 是的,它看起来解决了我的问题,希望我能够用空字符串替换这个提取的 SubSTR,非常感谢您在这方面的帮助.. 真的很感激
      • 再次感谢您的帮助,我能够用空字符串替换我的垃圾邮件文本。非常感谢您,希望我不会再次成为垃圾邮件的受害者
      • LOCATE() 也理解两个参数。所以不需要使用INSTR():dev.mysql.com/doc/refman/5.7/en/… "这和LOCATE的二元形式一样,只是参数的顺序颠倒了。"
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-03
      • 1970-01-01
      • 2013-07-25
      • 1970-01-01
      • 2011-01-30
      • 2011-04-22
      • 2020-11-13
      相关资源
      最近更新 更多