Jump to content

Parsing Text


onewaydom

Recommended Posts

Hello,

 

I am new to regular expressions. 

 

Problem: 

 

1. Remove all text before a certain string.

2. Replace something in the beginning string.

3. Replace something in the end of a string.

4. Be able to add a break after the end of each string.

5. Have this work on long strings repeating itself.

 

Example: 

 

Dealing with large text files as long directories. 

 

    i.e. onebiglongdirectory/prefix/somethingelsehere/suffix

          onebiglongdirectory/prefix/somethingelsehere/suffix

          onebiglongdirectory/prefix/somethingelsehere/suffix

 

Of course with space and other unnecessary text also included.

 

Solution:

 

1. I would like everything before the prefix dropped and replaced with a value that I input into a variable.

2. I would like the suffix dropped and replaced with a variable that I input as a variable.

3. Each directory on its own line.

 

Here is something I have created.  It works a bit, but stops short of doing the whole string of directories I put into it.

 

index.php

<html>

<head>

</head>

<body bgcolor = "#ACACAC">

<center>

<h1>Regular Expressions Engine</h1>

<h5>Enter the prefix that is to be changed along with the suffix, both new and old.</h5>

<form method=post action="expressions.php">

New Prefix 
<input type=text name="newprefix" size=40 style="background:#ECECEC"><br><br>

Old Prefix
<input type=text name="oldprefix" size=40 style="background:#ECECEC"><br><br>

New Suffix
<input type=text name="newsuffix" size=40 style="background:#ECECEC"><br><br>

Old Suffix

<input type=text name="oldsuffix" size=40 style="background:#ECECEC"><br><br>


Enter the string to be manipulated:<br><br>

<textarea name="content" rows=6 cols=40 style="background:#FFFDDA">

</textarea>

<br><br>

<input type=submit value="Process">  <input type="reset" value="Reset">

</form>

</center>

</body>
</html>

 

expressions.php

<?

  $content = trim( "$content"); 

  $firststring =strstr("$content", "$oldprefix");
  
  $string= str_replace("$oldprefix","$newprefix",$firststring);

  $second = str_replace("$oldsuffix","$newsuffix <br>",$string);


?>

    <html>
      <title>Regular Expressions Results</title>
         <body bgcolor = "#ACACAC">

           <center>
             <h1>Results</h1>

              <p><br><b></b></p>

              <p>

                <b><font color="#00A54E">

         <? 
            echo $second;
          ?> 
       </b></font>

</p>
<br>
<br>
<br>

<a href ="http://www.mydomain.com/regex">Back</a>

</center>

</body>
</html>

 

Any help appreciated.  I know the expressions I am looking for, but just don't know how to get them into php.

 

Replace oldsuffix to the end of the line with newsuffix

 

\.oldsuffix.*$  .newsuffix

 

Replace beginning of the line up to and including /oldprefix with /newprefix

 

^.*?/oldprefix           

 

 

Hope this makes sense!

 

Thanks

 

 

 

 

Link to comment
Share on other sites

1. Remove all text before a certain string.

2. Replace something in the beginning string.

3. Replace something in the end of a string.

4. Be able to add a break after the end of each string.

5. Have this work on long strings repeating itself.

 

1. /\A(.*?)(?=string)/s

2. /\Apattern/

3. /pattern\z/

4. /\s*\z/ replaced with a new line.

5. A string containing multiple lines?

Link to comment
Share on other sites

That sounds like the same thing to me, unless you're looping through a file line by line rather than slurping it all into one string. In either case, you're still using preg_replace.

 

I have the initial prefix and suffix being adjusted properly, but when I have more than one occurrence the function only takes care of the beginning and the end.

 

 $second = preg_replace(" /\A(.*?)($oldprefix?)/s", "$newprefix", "$content");
$third = preg_replace(" /$oldsuffix\z/", "$newsuffix", "$second");

 

I would like it to make a new line after each new suffix is added.  Additionally be able to do multiple lines of similar, but different text.  A long list if you will.

 

Link to comment
Share on other sites

Not sure if this is what you were meaning, but this works.

 

  $content = trim( "$content"); 

  $oldprefix = preg_quote($oldprefix, '/');

  $second = preg_replace(" /^(.*?)($oldprefix?)/sm", "$newprefix", "$content");
  
  $third= str_replace("$oldprefix","$newprefix",$second);

  $fourth = str_replace("$oldsuffix","$newsuffix <br>",$third);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.