Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace or delete a string segment in a text 1

Status
Not open for further replies.

aas1611

Programmer
Dec 14, 2001
184
DE
Hi all,

I have to delete (or replace with an empty string) a string segment in a text. Let's say:

$text = "Please see the previous publication (Journal 02/16/2006).";

I want to delete the "(Journal 02/16/2006)" segment, so it becomes:

$text = "Please see the previous publication.";

Can it be done in PHP? Or maybe using a MySQL function, because actually the text is saved in a table and always the ones with "(Journal...)" have to be deleted.

For all hints and helps, thanks!

Andre
 
assuming it is always the same pattern then php will do this for you quite neatly:
Code:
$text = "Please see the previous publication (Journal 02/16/2006).";
$text = substr($text,0, strpos($text,"(Journal"));
echo $text;
 
Hi,

I forgot to mention that, there could be more text or paragraph after the "(Journal 02/16/2006)."

Something like:
$text = "Please see the previous publication (Journal 02/16/2006). More text here...";

or
$text = "Please see the previous publication (Journal 02/16/2006). New paragraph here...";

I have tested your code, it deleted everything after "(Journal..)".

Andre
 
same principle applies.

Code:
$text = "Please see the previous publication (Journal 02/16/2006) more text then continues here.";
$start  = strpos($text,"(Journal");
$stop = strpos($text, ")", $start) + 1;
$text = substr($text,0, $start) . substr ($text, $stop);
echo $text;
 
It worked.

One more question, though. What about if there are many "(Journal...)" ? I tested it with more than one "(Journal...)" and it deleted only the first one.

I figure, the solution could be a function that can calculate how many times a particular segment appears or scan the whole text, then delete them. Maybe like:
Code:
while "(Journal...)" still exist {
   // your code from the previous reply here
}
I just can't find the right logic for while "(Journal...)" still exist.

Andre
 
you keep changing the goal posts! ;-)

there comes a time where the trade off between coding and regex shifts. the more you look for flexibility the more regex is a better solution than plain coding.

in this case i'd guess that coding is still better something like this should do it

Code:
$text = "Please see the previous publication (Journal 02/16/2006) more text then continues here.  There is another (Journal 02/10/90) reference here.";
echo parse($text);

function parse($text)
{
	while ($startpoint = strpos($text, "(Journal", 0)):
		$stop = strpos($text, ")", $startpoint) + 1;
		$text = substr($text,0, $startpoint) . substr ($text, $stop);
	endwhile;
	return $text;
}
 
Well, sorry. New cases come up after one is solved. Isn't it always like that? [smile]

I will give it a try, get back to you with the outcome.

Thanks!

Andre
 
Hmm..

If I copied your code on a new file (only and exactly the code you gave me), it worked just fine. But if I embedded it in my real PHP file, it didn't work.

The error message:

Fatal error: Call to undefined function: parse() in....


Do you know why?
Does it have to be a certain part or area of the file?
 
nope. it can be anywhere in the file (ish).

i would guess that you have pasted the function inside another function by mistake. which it wont ordinarily like if you're calling the parse function from outside the scope of the function in which it is embedded.

double check the position of the curly braces - i think you'll find that's the proble.
 
Yeah, the problem is, I call a database, and I put this code in the database scope. When I put the code outside the database scope, it works. I need to work with the database, though.

One more question:
at the end of the parse function, you wrote:

return $text

How to get the new value of $text outside the parse function after the parsing, so I can update it back into the database?

I've tried different ways, it came up with the old value.

You deserve a star for this!

Thanks!
Andre
 
the function returns the parsed text to whatever calls it. so
Code:
$parsedtext = parse($text);

this means that the output from the parsing function will be called $parsedtext. You can use this variable directly with a sql statement:

Code:
$parsedtext = parse($text);
$sql = "update tablename set textstring = '$parsedtext' where [primarykeycolumn] = 'value'";

Yeah, the problem is, I call a database, and I put this code in the database scope. When I put the code outside the database scope, it works. I need to work with the database, though.

by the "database scope" do you mean the function that creates the connection? i think the db connection and selection remain instantiated across the entire script unless you explicitly close them. thus you can connect and select in the first few lines of code and the connection and selection will be available to every other function within the script.

hth
Justin
 
What I mean by database scope, is from the opening database (mysql_connect) until the closing database (mysql_close). Somehow I can't put this function in the middle. So, here is what I do: I close the connection, work on the function, then open up the connection again.

Wondering if there is better way, though. But anyways, it works.

Thanks your help!

Andre
 
Hey do you happen to know exactly the opposite?

Only out of curiosity, if I want to insert a word segment between two particular words.

:)

Andre
 
no reason why the db code should stop it working (unless the function is inside another function). feel free to post the whole code if you want this bit fixed up.

yup - you can insert words but i am not sure what you mean by between? so if you had said can i insert a word after another word then that's easy. you just use str_replace() to replace all incidents of the "before" word with "before" + space + inserted word. if you want to restrict the insertion only to where you have a particular set of words you would use str_replace() to search for the words and add something in the middle:

Code:
$text = "this is the before after";

$first = "before"; //the start
$second = "after"; //the last word
$replace = "and"; //the text to be inserted

$text = str_replace ("$first $second", "$first $replace $second", $text);
echo $text;
 
OK. Here is an example of the real HTML text I have:

Code:
<p class=MsoListBullet><span style=\'font-family:Symbol;\'>·<span style=\'font:7.0pt \"Times New Roman\"\'></span></span>Billing central</p>

<p class=MsoListBullet><span style=\'font-family:Symbol;\'>·<span style=\'font:7.0pt \"Times New Roman\"\'></span></span>credit cards and</p>

more MsoListBullet or regular text here...

Then, I want to insert:

"<![if !supportLists]>" after "class=MsoListBullet>"

and

"<![endif]>" after "</span></span>"

As you see here, this MsoListBullet class can be more than one.

I have done the first one using your original code (with $starpoint and $stop). The second one is a little bit tricky because the words that come after "</span></span>" are always changing.

In the end, I want the HTML text to look like this:

Code:
<p class=MsoListBullet><![if !supportLists]><span style=\'font-family:Symbol;\'>·<span style=\'font:7.0pt \"Times New Roman\"\'></span></span><![endif]>Billing central</p>

<p class=MsoListBullet><![if !supportLists]><span style=\'font-family:Symbol;\'>·<span style=\'font:7.0pt \"Times New Roman\"\'></span></span><![endif]>credit cards and</p>

more MsoListBullet or regulat text here...

Can it be done?

Andre
 
Oh...
it works with str_replace! I didn't think that str_replace would search through all the text, but it did! Great!

Anyways, here is my complete code, see if you can fix it up (about the whole database call thing):

Code:
<?php

include("password.inc.php");
$conn=mysql_connect($Server, $Benutzer, $Passwort);
if ($conn){
	mysql_select_db($Database); 
	
	$query = " SELECT * FROM pressedienst_profis WHERE Artikel_ID = $ID ";
	$result = mysql_query($query);
	$rw = mysql_fetch_assoc($result);
	$ID = $rw['Artikel_ID'];
	$Html = $rw['Html'];	

	mysql_close();
}//Ende MYSQL

$text = $Html;

function parse($text) {
	while ($startpoint = strpos($text, "(VersicherungsJournal", 0)):
    	$stop = strpos($text, "/a>)", $startpoint) + 4;
    	$text = substr($text,0, $startpoint) . substr ($text, $stop);
    endwhile;
    return $text;
}
$parsedtext= parse($text);

$parsedtext1= str_replace("MsoListBullet><span","MsoListBullet><![if !supportLists]><span", $parsedtext);

$parsedtext2 = str_replace("</span></span>","</span></span><![endif]>", $parsedtext1);


include("password.inc.php");
$conn=mysql_connect($Server, $Benutzer, $Passwort);
if ($conn){
	mysql_select_db($Database); 
	
	echo"
	<html>
	<head>
	<title>Verwaltung des Versichungs Journals</title>
	<link href='verwaltung.css' type=text/css rel=stylesheet>
	<style type='text/css'>
	<!--
	body, td{font-family:arial,sans-serif;font-size:11pt;text-align:middle;}
	-->
	.lang {width:405px}
	.Button
   	{ background-color:#624AFA; color:#FFFFFF; width:250px; border:3px solid #DDDDDD; }
   .Button2
   { background-color:#624AFA; color:#FFFFFF; width:160px; border:3px solid #DDDDDD; }</style>
	</head>
	<body>";
	
	$query = " UPDATE pressedienst_profis SET Html = '$parsedtext2' WHERE Artikel_ID = $ID ";
	$result = mysql_query($query);
	if ($result) {
		echo "<br><br><b>Textänderung (Artikel: $ID) erfolgreich gespeichert.</b><br>"; 
		echo "Zurück zu der <a href=default_pressedienst_profis.php>Profi-Pressedienst Haupseite.</a>";
	} else echo "Fehler aufgetreten.";

	echo '</body></html>';

	mysql_close();
}//Ende MYSQL		
?>

There is include function, but the password.inc only contains password and user info. Not more, no function in it.

Andre
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top