Reply
pulling out a part of a string
Old 03-26-2006, 11:37 AM pulling out a part of a string
numbenator's Avatar
Ultra Talker

Posts: 411
Location: London
HI

I want to extract text between two points of a string

the string is held in variable $extracted and the text u i wish to get hold of is between the below anchor points

<!-- start -->

<!-- end -->

How do i do this?

cheers
__________________
www.mastech-solutions.com
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
When You Register, These Ads Go Away!
Old 03-26-2006, 03:22 PM Re: pulling out a part of a string
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
PHP Code:
<?php

$extracted 
"before<!-- start -->between<!-- end -->after";

preg_match("#<!-- start -->(.*?)<!-- end -->#",$extracted,$matches);

print 
$matches[1];

?>
Ought to do it.

EDIT, d'oh, it should be $matches[1]....
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';
Scribble Pad MOD for phpBB (aka MSN handwriting for forums)

Last edited by 0beron : 03-27-2006 at 06:48 PM.
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 03-27-2006, 04:03 AM Re: pulling out a part of a string
numbenator's Avatar
Ultra Talker

Posts: 411
Location: London
Hi There,
Your script is not actually separating the section i require. as you can see on the link below, the whole page is extracted and not the code between the anchor points start and end which basiclaly is the left hand column of the page.

http://www.pnets.org/php/HTMLEmail.php
any more ideas?
__________________
www.mastech-solutions.com

Last edited by numbenator : 03-27-2006 at 04:14 AM.
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
Old 03-27-2006, 05:38 AM Re: pulling out a part of a string
Super Talker

Posts: 144
Code:
<?
  // get the data from site
  if ($handle = fopen("http://www.pnets.org/php/HTMLEmail.php", "r")) {
    $contents = '';
    while (!feof($handle)) {
      $contents .= fread($handle, 8192);
    }
    fclose($handle);
  }
  else $contents = "";
 
  // here's the code
  if (preg_match_all("/<!-- start -->(.*?)<!-- end -->/is",$contents,$matches)) {
    $regCount = -1;
    while (isset($matches[1][++$regCount])) echo $matches[1][$regCount] ."\n\n\n\n\n\n\n\n\n";
  }
?>
__________________
create.vibe
web development portfolio
createvibe.com is offline
Reply With Quote
View Public Profile Visit createvibe.com's homepage!
 
Old 03-27-2006, 08:17 AM Re: pulling out a part of a string
numbenator's Avatar
Ultra Talker

Posts: 411
Location: London
hi Vibe,
This is great, but I want to grab that string and write it somewhere else. (another file)

How can i use this script to reflect this.

Sorry. well new to this php.


Basically. The html that this script extracts, i wish to write to another html page.

cheers
__________________
www.mastech-solutions.com
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
Old 03-27-2006, 02:08 PM Re: pulling out a part of a string
Super Talker

Posts: 144
Code:
<?
  $html_code_to_write = "";
 
  // here's the code
  if (preg_match_all("/<!-- start -->(.*?)<!-- end -->/is",$contents,$matches)) {
    $regCount = -1;
    while (isset($matches[1][++$regCount]))
      $html_code_to_write .= $matches[1][$regCount] ."\n\n\n\n\n\n\n\n\n";
  }
 
  if ($fp = fopen("/path/to/my/file.txt", "w")) {
?>
__________________
create.vibe
web development portfolio
createvibe.com is offline
Reply With Quote
View Public Profile Visit createvibe.com's homepage!
 
Old 03-27-2006, 02:09 PM Re: pulling out a part of a string
Super Talker

Posts: 144
if I understand you correctly...

Code:
<?
  $html_code_to_write = "";
 
  // here's the code
  if (preg_match_all("/<!-- start -->(.*?)<!-- end -->/is",$contents,$matches)) {
    $regCount = -1;
    while (isset($matches[1][++$regCount]))
      $html_code_to_write .= $matches[1][$regCount] ."\n\n\n\n\n\n\n\n\n";
  }
 
  if ($fp = fopen("/path/to/my/file.txt", "w")) {
    fputs($fp, $html_code_to_write, strlen($html_code_to_write));
    fclose($fp);
  }
?>
__________________
create.vibe
web development portfolio
createvibe.com is offline
Reply With Quote
View Public Profile Visit createvibe.com's homepage!
 
Old 03-27-2006, 02:10 PM Re: pulling out a part of a string
Super Talker

Posts: 144
bah, I think I hit tab or something and with the keypress, it submitted the first bit..

sowwy
__________________
create.vibe
web development portfolio
createvibe.com is offline
Reply With Quote
View Public Profile Visit createvibe.com's homepage!
 
Old 03-27-2006, 06:48 PM Re: pulling out a part of a string
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Woops, the first match ($matches[0]) is the whole match, not the first group...
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';
Scribble Pad MOD for phpBB (aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 03-27-2006, 06:51 PM Re: pulling out a part of a string
Super Talker

Posts: 144
Quote:
Originally Posted by 0beron
Woops, the first match ($matches[0]) is the whole match, not the first group...
true, but it also needed the /s modifier so the '.' char included newlines in the regexp
__________________
create.vibe
web development portfolio
createvibe.com is offline
Reply With Quote
View Public Profile Visit createvibe.com's homepage!
 
Reply     « Reply to pulling out a part of a string
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.16016 seconds with 12 queries