Heppy Posted October 18, 2010 Share Posted October 18, 2010 Hey, I'm doing a thing for work, and I need to strip out a <li> tag from a source, The li tag looks like this: <li class="g" id="mbbDD">CONTENT HERE WITH TAGS NEWLINES ETC</li> Where the DD in ID is random Numbers so any numbers should do. <li class="g c35"> <h3 class="r"> <a href="http://www.massagestockholm.net/" class="l"><em>Massage Stockholm</em></a> </h3> <div class="s"> <em>Massage Stockholm</em> - Hitta och boka alla typer av massage i Stockholm. Enbart Bokadirekt-anslutna mass�rer. Thaimassage, Taktilmassage, Shiatsumassage <b>...</b><br> <span class="f"><cite>www.<b>massagestockholm</b>.net/</cite> - <span class="gl"><a href="http://webcache.googleusercontent.com/search?q=cache:YROKHhfXogcJ:www.massagestockholm.net/+massage+stockholm&cd=8&hl=sv&ct=clnk&gl=se&ie=UTF-8">Cachad</a></span></span> </div> </li> <li class="g c35"> <h3 class="r"> <a href="http://www.massagestockholm.net/Bookings/Default.aspx?sru=42" class="l">MASSAGEN vid Centralbadet i Stockholm - <em>Massage Stockholm</em></a> </h3> <div class="s"> <em>Massage</em> i <em>Stockholm</em> City bredvid Centralbadet Drottninggatan, boka <em>massage</em> <b>...</b><br> <span class="f"><cite>www.<b>massagestockholm</b>.net/Bookings/Default.aspx?sru...</cite> - <span class="gl"><a href="http://webcache.googleusercontent.com/search?q=cache:R8fK37N8IxAJ:www.massagestockholm.net/Bookings/Default.aspx%3Fsru%3D42+massage+stockholm&cd=9&hl=sv&ct=clnk&gl=se&ie=UTF-8">Cachad</a></span></span> </div> </li> <li class="g" id="mbb10"> <h3 class="r"> <a href="http://www.massagestockholm.net/Bookings/Default.aspx?sru=551" class="l">Kroppia - <em>Massage Stockholm</em></a> </h3> <div class="s"> <em>Massage Stockholm</em>? Rehabilitering Stockholm? Vid St Eriksplan. V�lkommen <b>...</b><br> <span class="f"><cite>www.<b>massagestockholm</b>.net/Bookings/Default.aspx?sru...</cite> - <span class="gl"><a href="http://webcache.googleusercontent.com/search?q=cache:a0T8UoaQt3IJ:www.massagestockholm.net/Bookings/Default.aspx%3Fsru%3D551+massage+stockholm&cd=10&hl=sv&ct=clnk&gl=se&ie=UTF-8">Cachad</a></span></span> </div> <div class="mbl bl"> <span class="ch c36" id="mbl10" onclick="google.x(this)"></span> <div class="mbi"> <span class="ch c36" id="mbl10" onclick="google.x(this)"></span> </div><span class="ch c36" id="mbl10" onclick="google.x(this)"><a href="#" onclick="return false" class="mblink">Visa fler resultat fr�n massagestockholm.net</a></span> </div> <div id="mbf10"></div> </li> <li class="g"> <h3 class="r"> <a href="http://www.sthlmphysique.se/" class="l"><em>Massage</em> i <em>Stockholm</em>, Vasastan - Sthlm Physique - �ven presentkort</a> </h3> <div class="s"> STHLM Physique - Erbjuder <em>massage</em> mitt i Vasastan, <em>Stockholm</em>, med certifierade terapeuter. Best�ll <em>massage</em> presentkort direkt p� hemsidan. V�lkommen!<br> <span class="f"><cite>www.sthlmphysique.se/</cite> - <span class="gl"><a href="http://webcache.googleusercontent.com/search?q=cache:1E4rvRvU_t4J:www.sthlmphysique.se/+massage+stockholm&cd=11&hl=sv&ct=clnk&gl=se&ie=UTF-8">Cachad</a> - <a href="/search?hl=sv&ie=UTF-8&q=related:www.sthlmphysique.se/+massage+stockholm&tbo=1&sa=X&ei=eiq8TOvVDIKfOtHplIcN&ved=0CEsQHzAK">Liknande</a></span></span> </div> </li> <li class="g"> adam </li> <li>abscfgef</li> <li>soh3278462378</li> <li> <a href="test.php">test</a> </li> <li> </li> This is my "solution" i've come up with but it doesn't work as I'd like. It removes the designated tag AND everything after =/... Any ideas? preg_replace('/<li class="g" id="mbb[\d]*">(.*|\s*)*<\/li>/', "", $content); //Heppy. Quote Link to comment https://forums.phpfreaks.com/topic/216161-stripping-html-tags/ Share on other sites More sharing options...
JonnoTheDev Posted October 18, 2010 Share Posted October 18, 2010 http://uk.php.net/strip_tags Quote Link to comment https://forums.phpfreaks.com/topic/216161-stripping-html-tags/#findComment-1123402 Share on other sites More sharing options...
Heppy Posted October 18, 2010 Author Share Posted October 18, 2010 That'll only strip non allowed tags. I wish to only remove the tags with certain ID's & Class, Which I don't think strip_tags can do.. Quote Link to comment https://forums.phpfreaks.com/topic/216161-stripping-html-tags/#findComment-1123419 Share on other sites More sharing options...
JonnoTheDev Posted October 18, 2010 Share Posted October 18, 2010 You are looking from the wrong angle. What you need to look at is what data do you want to extract, not what you want to get rid of. Can you give a more detailed explanation. I cannot tell from your OP what you want from the source. Quote Link to comment https://forums.phpfreaks.com/topic/216161-stripping-html-tags/#findComment-1123422 Share on other sites More sharing options...
Heppy Posted October 18, 2010 Author Share Posted October 18, 2010 kk, There's a list of <li> tags Some of the <li> tags has class, some of it has CLASS AND ID <li class="g"></li> <li class="g"><li> <li class="g" id="mbb50"></li> <li class="g"></li> <li class="g"></li> I want to get all the <li> elements with ONLY a class, NOT an ID. Quote Link to comment https://forums.phpfreaks.com/topic/216161-stripping-html-tags/#findComment-1123433 Share on other sites More sharing options...
JonnoTheDev Posted October 18, 2010 Share Posted October 18, 2010 <?php $string = ' <li class="g c35"> <h3 class="r"> <a href="http://www.massagestockholm.net/" class="l"><em>Massage Stockholm</em></a> </h3> <div class="s"> <em>Massage Stockholm</em> - Hitta och boka alla typer av massage i Stockholm. Enbart Bokadirekt-anslutna mass�rer. Thaimassage, Taktilmassage, Shiatsumassage <b>...</b><br> <span class="f"><cite>www.<b>massagestockholm</b>.net/</cite> - <span class="gl"><a href="http://webcache.googleusercontent.com/search?q=cache:YROKHhfXogcJ:www.massagestockholm.net/+massage+stockholm&cd=8&hl=sv&ct=clnk&gl=se&ie=UTF-8">Cachad</a></span></span> </div> </li> <li class="g c35"> <h3 class="r"> <a href="http://www.massagestockholm.net/Bookings/Default.aspx?sru=42" class="l">MASSAGEN vid Centralbadet i Stockholm - <em>Massage Stockholm</em></a> </h3> <div class="s"> <em>Massage</em> i <em>Stockholm</em> City bredvid Centralbadet Drottninggatan, boka <em>massage</em> <b>...</b><br> <span class="f"><cite>www.<b>massagestockholm</b>.net/Bookings/Default.aspx?sru...</cite> - <span class="gl"><a href="http://webcache.googleusercontent.com/search?q=cache:R8fK37N8IxAJ:www.massagestockholm.net/Bookings/Default.aspx%3Fsru%3D42+massage+stockholm&cd=9&hl=sv&ct=clnk&gl=se&ie=UTF-8">Cachad</a></span></span> </div> </li> <li class="g" id="mbb10"> <h3 class="r"> <a href="http://www.massagestockholm.net/Bookings/Default.aspx?sru=551" class="l">Kroppia - <em>Massage Stockholm</em></a> </h3> <div class="s"> <em>Massage Stockholm</em>? Rehabilitering Stockholm? Vid St Eriksplan. V�lkommen <b>...</b><br> <span class="f"><cite>www.<b>massagestockholm</b>.net/Bookings/Default.aspx?sru...</cite> - <span class="gl"><a href="http://webcache.googleusercontent.com/search?q=cache:a0T8UoaQt3IJ:www.massagestockholm.net/Bookings/Default.aspx%3Fsru%3D551+massage+stockholm&cd=10&hl=sv&ct=clnk&gl=se&ie=UTF-8">Cachad</a></span></span> </div> <div class="mbl bl"> <span class="ch c36" id="mbl10" onclick="google.x(this)"></span> <div class="mbi"> <span class="ch c36" id="mbl10" onclick="google.x(this)"></span> </div><span class="ch c36" id="mbl10" onclick="google.x(this)"><a href="#" onclick="return false" class="mblink">Visa fler resultat fr�n massagestockholm.net</a></span> </div> <div id="mbf10"></div> </li> <li class="g"> <h3 class="r"> <a href="http://www.sthlmphysique.se/" class="l"><em>Massage</em> i <em>Stockholm</em>, Vasastan - Sthlm Physique - �ven presentkort</a> </h3> <div class="s"> STHLM Physique - Erbjuder <em>massage</em> mitt i Vasastan, <em>Stockholm</em>, med certifierade terapeuter. Best�ll <em>massage</em> presentkort direkt p� hemsidan. V�lkommen!<br> <span class="f"><cite>www.sthlmphysique.se/</cite> - <span class="gl"><a href="http://webcache.googleusercontent.com/search?q=cache:1E4rvRvU_t4J:www.sthlmphysique.se/+massage+stockholm&cd=11&hl=sv&ct=clnk&gl=se&ie=UTF-8">Cachad</a> - <a href="/search?hl=sv&ie=UTF-8&q=related:www.sthlmphysique.se/+massage+stockholm&tbo=1&sa=X&ei=eiq8TOvVDIKfOtHplIcN&ved=0CEsQHzAK">Liknande</a></span></span> </div> </li> <li class="g"> adam </li> <li>abscfgef</li> <li>soh3278462378</li> <li> <a href="test.php">test</a> </li> <li> </li>'; preg_match_all("(<li class=\"g(.*)</li>)siU", $string, $matches); $result = $matches[0]; $parts = array(); foreach($result as $li) { if(!strstr($li, 'id="')) { $parts[] = $li; } } foreach($parts as $li) { print $li; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/216161-stripping-html-tags/#findComment-1123448 Share on other sites More sharing options...
Heppy Posted October 18, 2010 Author Share Posted October 18, 2010 Nice! Thanks alot! Worked like a charm Quote Link to comment https://forums.phpfreaks.com/topic/216161-stripping-html-tags/#findComment-1123449 Share on other sites More sharing options...
Heppy Posted October 19, 2010 Author Share Posted October 19, 2010 Remade the your script and use the filter version and modified it a bit and added the class="g" & id="mbb And it worked like a charm, now I get the same result as your script instead I just do it one one line of code Couldn't have done it without your regexp thou, as that was the part that made it work. Quote Link to comment https://forums.phpfreaks.com/topic/216161-stripping-html-tags/#findComment-1123759 Share on other sites More sharing options...
Heppy Posted October 19, 2010 Author Share Posted October 19, 2010 Remade the your script and use the filter version and modified it a bit and added the class="g" & id="mbb And it worked like a charm, now I get the same result as your script instead I just do it one one line of code Couldn't have done it without your regexp thou, as that was the part that made it work. oh yeah, used the preg_replace() Quote Link to comment https://forums.phpfreaks.com/topic/216161-stripping-html-tags/#findComment-1123771 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.