Jump to content


Photo

subtract string from string


  • Please log in to reply
2 replies to this topic

#1 dfrojd

dfrojd
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 02 September 2006 - 07:38 PM

Hi folks,

sorry for a surely simple question but (I swear) I have tried all normal sources for hours now and cant find what I need (sob).

I just simply need to subtract a part of a string from a larger string and keep the remainder.

The strings involved contain html code with lots-o-tags and must stay that way.

maybe can someone help.

The code in question finds two parts of a string using a regex.
No all I need is to subtract the two found strings from the original and keep the rest.

<?php
$teststr = "<table cellpadding=5 cellspacing=0><tr><td align=center width=358 nowrap style=\"color:#CCDDCC\">\n";
$teststr .= "<img src=\"Karten/Regionen/164-22-11-Armenime.gif\" alt=\"Lage von Armenime auf den Kanarischen Inseln\" \n";
$teststr .= "width=350 height=150 class=r><br>Lage auf den Kanarischen Inseln</td></tr></table>\n";
$teststr .= "    <br>\n";
$teststr .= "    <br>\n";
$teststr .= "    <br>\n";
$teststr .= "    <span class=g12f>Detail-Informationen zu diesem Angebot</span>\n";
$teststr .= "    <div class=r style=\"margin-top:12px;\">\n";
$teststr .= "      <strong>Finca Constanza mit Pool - Ruhe - Luxus - Golf - Aloe-vera - Erholung - Sonne\n";
$teststr .= "</strong><br>\n";
$teststr .= "      <br>\n";
$teststr .= "      6 Wohneinheiten auf 10.000 qm Finca mit Pool ***** Herrenhaus 204m2 -9 Pers. ***** Patio Suite -8 Pers.\n";
$teststr .= "***** Bungalow, Cesar -6 Pers. ***** Apartment, Casita Sira 4 Pers. ***** Bungalow, Cleopatra -4 Pers. *****\n";
$teststr .= "Wintergartenhaus -3 Pers<br>\n";
$teststr .= "      <br>\n";
$teststr .= "      Detailinfos macht Siggi Detailinfos macht Siggi Detailinfos macht Siggi Detailinfos macht Siggi\n";
$teststr .= "Detailinfos macht Siggi Detailinfos macht Siggi Detailinfos macht Siggi Detailinfos macht Siggi\n";
$teststr .= "Detailinfos macht Siggi Detailinfos macht Siggi Detailinfos macht Siggi Detailinfos macht Siggi \n";
$teststr .= "Detailinfos macht Siggi Detailinfos macht Siggi Detailinfos macht Siggi Detailinfos macht Siggi \n";
$teststr .= "Detailinfos macht Siggi Detailinfos macht Siggi Detailinfos macht Siggi Detailinfos macht Siggi \n";
$teststr .= "Detailinfos macht Siggi Detailinfos macht Siggixxx\n";
$teststr .= "    </div>\n";
$teststr .= "    <br>\n";
$teststr .= "    <br>\n";
$teststr .= "    <span class=g12f>Besonderheit Titel 1</span>\n";
$teststr .= "    <div class=r>\n";
$teststr .= "        Besonderheit: Beschreibung: 1\n";
$teststr .= "    </div>\n";
$teststr .= "    <br>\n";
$teststr .= "    <br>\n";
$teststr .= "    <span class=g12f>Besonderheit Titel 2</span>\n";
$teststr .= "    <div class=r>\n";
$teststr .= "      Besonderheit: Beschreibung: 2\n";
$teststr .= "    </div>\n";
$teststr .= "    <br>\n";
$teststr .= "    <br>\n";
$teststr .= "    <span class=g12f>Besonderheit Titel 3</span>\n";
$teststr .= "    <div class=r>\n";
$teststr .= "      Besonderheit: Beschreibung: 3\n";
$teststr .= "    </div>\n";
$teststr .= "    <br>\n";
$teststr .= "    <br>\n";
$teststr .= "    <span class=g12f>Urlaubsort Armenime</span>\n";
$teststr .= "    <div class=r>\n";
$teststr .= "      Spanien\n";
$teststr .= "      <span class=g10>&nbsp;&gt;&nbsp;</span> Kanarische Inseln\n";
$teststr .= "      <span class=g10>&nbsp;&gt;&nbsp;</span> Insel Teneriffa-Süd\n";
$teststr .= "      <span class=g10>&nbsp;&gt;&nbsp;</span> Armenime\n";
$teststr .= "<br><br>nfos zum<br>Urlaubsort: -... -Die von der Hauptstrasse ca. 400 m abseits und sehr ruhig gelegene Finca\n";
$teststr .= "Constanza bietet unseren Gästen optimalen Erholungsgenuss. Die geteerte, schmale Privatstrasse endet an der\n";
$teststr .= "Finca, sodass kein Durchgangsverkehr besteht. Da auch kein Baumaschinen-Lärm in der näheren Umgebung\n";
$teststr .= "stört, wird das Fincagelände den Ruhe suchenden Gästen in jeder Weise gerecht.\n";
$teststr .= "    </div>\n";
$teststr .= "    <div style=\"text-align:right; width:600px;\">&nbsp;<a href=\"#Anreise-Armenime\" class=f8>Anreise nach Armenime</a></div>\n";
$teststr .= "    <br>\n";
$teststr .= "    <span class=g12f>Freizeitangebote in der Umgebung</span>	\n";

$content = $teststr;

// DETAILINFOS -------------------------------------------------- FD ist sozusagen die vordere Grenze....
preg_match("=Detail-Informationen zu diesem Angebot(.*)<\/div>=siU", $content, $hit);
$data['obj_detail'] = trim($hit[1]);
echo "obj_detail = ".$data['obj_detail']."<br>\n";

// ORT INFO -------------------------------------------------- FD ist sozusagen die hintere Grenze....
preg_match("=<span class\=g12f>Urlaubsort(.*)<\/div>=siU", $content, $hit);
$data['obj_ort_info'] = trim($hit[1]);
echo "obj_ort_info = ".$data['obj_ort_info']."<br>\n";
?>

MOD EDIT - code tags added

#2 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 02 September 2006 - 07:55 PM

have you tried substr()
http://es2.php.net/m...tion.substr.php
Check the other string functions there as well
Tell me the problem, I will try tell you the solution

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 02 September 2006 - 08:09 PM

Does this help?

<?php
function subtractString ($original, $remove) {
    $len = strlen($remove);
    $pos1 = strpos($original, $remove);
    $res = substr($original,0,$pos1) . substr($original,$pos1+$len);
    return $res;
}

$teststr = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nulla lacinia justo at velit. Nunc porttitor nisl porta mi. Curabitur sollicitudin purus vel felis. Maecenas nunc. Nulla facilisi. Integer vel ante sit amet ipsum consequat facilisis. Suspendisse hendrerit urna vel erat. Quisque auctor, erat nec ullamcorper sollicitudin, wisi leo hendrerit nisl, eu auctor turpis justo quis justo. Proin arcu metus, commodo quis, tincidunt varius, fermentum eget, lectus. Ut tellus. Sed nec dolor. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nam sed magna.";

$str1 = "Curabitur sollicitudin purus vel felis.";
$str2 = "Proin arcu metus, commodo quis, tincidunt varius, fermentum eget, lectus. Ut tellus.";

echo "<p>$teststr</p>";
$result = subtractString($teststr, $str1);
$result = subtractString($result, $str2);
echo "<p>$result</p>";
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users