I get source of some files. Then i need to extract fragments of text from there. What functions do you recommend to use?
Jake Arkinstall
Thank you. This samples will help me in solving the task. I think it would be a little bothering to you to read hole source cause half of it is in ukrainian language as i am ukrainian:D. Moreover i am going to use a few completely different sources.
:)regards
Its just what i think i wont try it. Thank you.
Theoretically you could use it without, but I won’t recommend it.
For example, imagine this is your main string:
Here is an accidental ending piece {/text} but here is {text}some text for you{/text} to read!
Now, with the offset set as the beginning of the string, then it will look for the next occurrence of the end tag.
Without an offset, it would find the first occurrence of the end tag which could possibly be before the initial tag, causing unexpected results.
Try it out
Sorry for the theoretical question to the back sample. Do i correctly understand that $BeginPosition here is offset from which function starts to search position of the $End and could it possible not to use it?
$EndPosition = strpos($String, $End, $BeginPosition);
Sure thing.
Something like the following?
preg_match_all('~<img[^>]* src="([^"]+)"~', $String, $Matches);
var_dump($Matches);
Basically checks for an image tag, allows more attributes etc but no closing > character. Checks for source and grabs everything between two quotes.
You can use the $Matches array to extract what you want.
Jake Arkinstall i want to thank you once more. Second sample works perfectly. Though first i didnt try yet.
And now i have second task to extract one path to image from img tag in source. Could you help with it?
“If you want to grab text between two things, for example, you could find the position of the first thing, the position of the second and use substr to grab it.”
Its just what i seek. Thanks. Could you write a little sample?
It would help vastly if you could give us an example of the files, and the text you need to extract.
For example, if you want to grab all of the content of a tag with a certain ID, you’d parse the HTML with a lenient XML parser and use DOM accessing methods to retrieve the content you’re after.
If you want to grab a fixed amount of text starting at a fixed position, you’d use the substr function.
If you want to grab text between two things, for example, you could find the position of the first thing, the position of the second and use substr to grab it.
The most common approach, however, is with regular expressions. For that you’d use the Preg library (unfortunately many people still use the deprecated Ereg library, so keep away from tutorials which use ereg_* functions if looking up regex (regular expressions)).
That is all I can really say without an example to work with.
Well, a Regular Expression approach could be:
<?php
$String = 'here is {text}some text for you{/text} to read';
$Begin = '{text}';
$End = '{/text}';
preg_match("~{$Begin}(.+){$End}~", $String, $Matches);
$Snippet = $Matches[1];
echo $Snippet;
And a plain text-based function approach could be:
<?php
$String = 'here is {text}some text for you{/text} to read';
$Begin = '{text}';
$End = '{/text}';
$BeginPosition = strpos($String, $Begin) + strlen($Begin);
$EndPosition = strpos($String, $End, $BeginPosition);
$Snippet = substr($String, $BeginPosition, $EndPosition - $BeginPosition);
echo $Snippet;
Again, however, without example I can’t provide a good quality code sample for you because it varies massively depending on your situation.