FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


An Easier Way To Search An Array? Need Help Please.





Dougie1
Well I have my file loaded into an array, using file() and now I would like it to print only the info between <important> and </important>. These are on different lines and amongst other info Like the following:

Code:
Array
(
    [0] => sdfoiaowthsfs
    [1] => sdfsdf<important>sffokjskod
    [2] => useful info
    [3] => useful info
    [4] => useful</important>sdfijsd
    [8] => sdfsdfsdf
    [9] => sdfsdfsdg
)


So how would I do this in as few steps as possible. Thanks Smile
sonam
You can use array search function.

http://hr.php.net/manual/en/function.array-search.php

Sonam
Dougie1
But how would I then be able to print all of the stuff in between <important></important> because all it outputs it the key.

Also doesn't it search for an array containing the whole part ie it wouldn't find anything when I ran it on my array above because no part of my individually contains <important> it contains text around about <important>?
Aredon
Simply implode the array to a string then toss it against a regular expression. The line breaks will remain intact since the file function doesn't actually remove them.
Code:

<?php
$filearray=Array(
0 => "sdfoiaowthsfs\r\n",
1 => "sdfsdf<important>sffokjskod\r\n",
2 => "useful info\r\n",
3 => "useful info\r\n",
4 => "useful</important>sdfijsd\r\n",
5 => "sdfsdfsdf\r\n",
6 => "sdfsdfsdf\r\n",
7 => "sdfsdfsdf\r\n",
8 => "sdfsdfsdf\r\n",
9 => "sdfsdfsdg\r\n"
);
$file_get_contents_equivalent=implode("", $filearray);
$regexstr="/<important>(.*)<\/important>/s";
preg_match($regexstr,$file_get_contents_equivalent,$r);
echo '<pre>'.htmlentities($r[1]).'</pre>';
?>
smartpandian
yes.. I think that'll do..

Also, Php has got functions for extracting content from html/xml files using DOM architecture.


It would be straight forward to use that functions.
sonam
How are you get this array? It is not looking like something useful. If you get this array with file function e.g.
$filearray = file("somehtml.html");

then this is wrong, and you must go in different way to get out part of some html page.

Also, I don't understand, did you like to print out all (if you have more then one) values beetween <important> and </important> or just values what contain searched string.

Remove all tags from some file exept <important>:

Code:
$file = "proba.htm"; // some file
$onlyimp = strip_tags(file_get_contents($file),'<important>');
echo $onlyimp;


Sonam
Dougie1
Aredon wrote:
Simply implode the array to a string then toss it against a regular expression. The line breaks will remain intact since the file function doesn't actually remove them.
Code:

<?php
$filearray=Array(
0 => "sdfoiaowthsfs\r\n",
1 => "sdfsdf<important>sffokjskod\r\n",
2 => "useful info\r\n",
3 => "useful info\r\n",
4 => "useful</important>sdfijsd\r\n",
5 => "sdfsdfsdf\r\n",
6 => "sdfsdfsdf\r\n",
7 => "sdfsdfsdf\r\n",
8 => "sdfsdfsdf\r\n",
9 => "sdfsdfsdg\r\n"
);
$file_get_contents_equivalent=implode("", $filearray);
$regexstr="/<important>(.*)<\/important>/s";
preg_match($regexstr,$file_get_contents_equivalent,$r);
echo '<pre>'.htmlentities($r[1]).'</pre>';
?>


Thanks for that. Implode is really useful. Can I just ask how you understand the regexstr bit. It looks like a lot of random characters to me and the php manual didn't explain it too well.

How would I go about making my own preg_match expressions?
powers1983
http://uk.php.net/manual/en/reference.pcre.pattern.syntax.php

You're right - it is confusing. I'm new to regular expressions but I read enough to be able to understand what the one above does:
Code:

$regexstr="/<important>(.*)<\/important>/s";


the first / is the delimiter - basically the start and stop of the expression.

Then the <important> bit means that the preg_match will only match if that is found first.

Then then (.*) bit is a 'sub-pattern' (thats what the brackets mean) of .* which is in two parts : the . means match any character expect a newline (by default). The * means match as many as you can.

Then the <\/important> bit defines the end of what you want - the / is escaped by the \ because it is the delimiter and we don't want to end yet.

Not entirely sure what the /s does - the / is the delimiter which tells it that its the end of the expression but you'll need to wait til someone else can explain in a wee bit more depth.

Hope that helps.

David.
Aredon
The ending "s" is a modifier telling PHP to change the default functionality of the dot to include linebreaks.
(which the . [wild character I tend to call it] by default doesn't match line breaks)

Could also write it like this:
Code:

$regexstr="/<important>([\s\S]*)<\/important>/";
Related topics
help! please
I need help, please
Admins help please, My domain wont work! (its my domain.)
CSS/Layout Help Please!
my site?
Major Problem with COmputer. Help Please
problems with my comp....something strange, please help me
Started to use my own coding!!! HELP please!
PHP Help please
I am having alot of trouble with forms! Urgent help please!
PLEASE HELP!!.......PLEASE
help please
Help please! I can't login to my blog admin panel
Hmmmm, Favicon not working, check it out and help please
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.