...

View Full Version : Get in between two symbols



Jazz914
10-23-2009, 09:53 PM
I want to be able to get a value in between two symbols, can be any, for example:

and it would loop through the document and find a place where something starts with "[" and ends with "]" (for verification purposes, so that things like "[" with [B]no "]") and the returned information would be "blah", and the information would be storred in an array.

I am looking to use this with file_get_contents, I get some of it, I just don't know how to search for something in this way efficiently or even at all xD

Fumigator
10-23-2009, 10:45 PM
This is a perfect case for a regular expression.

http://www.regular-expressions.info/php.html

If you become overwhelmed, you are in the right place. regex is an advanced programming concept.

Jazz914
10-24-2009, 12:33 AM
thanks, I was thinking of a regex, just don't know how I would create one, so i'll have a look at the link you provided and get back to you ^_^

Jazz914
10-24-2009, 01:42 AM
Okay, I have figured out the regex, but I don't understand how I would use this to extract the information I need, I thought of an example..
"{blah}" would be replaced with something in a file found at "root/site/something/blah.php" and not [blah.php] or something
the regex I tested with is the following:


$blah = ("{BLAH} my name is Jazz!!!");
$something = preg_replace("%{(.*?)}%", "FOUND", $something);
echo($something);

and it replaces "{BLAH}" with FOUND, but this isn't what I want as the thing which would need to be changed will be a dynamic value? (Which is between the {...} whilst still getting rid of the braces)

oesxyl
10-24-2009, 02:09 AM
Okay, I have figured out the regex, but I don't understand how I would use this to extract the information I need, I thought of an example..
"[blah]" would be replaced with something in a file found at "root/site/something/blah.php" and not [blah.php] or something
the regex I tested with is the following:


$blah = ("{BLAH} my name is Jazz!!!");
$something = preg_replace("%{(.*?)}%", "FOUND", $something);
echo($something);

and it replaces "{BLAH}" with FOUND, but this isn't what I want as the thing which would need to be changed will be a dynamic value?



$blah = "{BLAH} my name is Jazz!!!";
$something = preg_replace("/\{([^\}]+)\}/","$1",$something);
echo $something;


read both PCRE and Posix Regex from this link:

http://www.php.net/manual/en/refs.basic.text.php

there are also some examples for both.

best regards

Jazz914
10-24-2009, 02:40 AM
Thanks thats one step closer to the thing I need, but I am trying to replace the thing within the braces with information from a file, for example this is how I would of thought it
works..

$something = preg_replace("/\{([^\}]+)\}/", file_get_contents($1.".php"), $blah); But it errors, so I tried this:

$something = preg_replace("/\{([^\}]+)\}/", file_get_contents("$1.php"), $blah); But then it actually looks for a file named "$1"

oesxyl
10-24-2009, 02:58 AM
Thanks thats one step closer to the thing I need, but I am trying to replace the thing within the braces with information from a file, for example this is how I would of thought it
works..

$something = preg_replace("/\{([^\}]+)\}/", file_get_contents($1.".php"), $blah); But it errors, so I tried this:

$something = preg_replace("/\{([^\}]+)\}/", file_get_contents("$1.php"), $blah); But then it actually looks for a file named "$1"


$filename = preg_replace("/\{([^\}]+)\}/", "$1", $blah);
$filecontent = file_get_contents($filename . ".php");
echo "[" . $filecontent . "]"; // ???


best regards

Jazz914
10-24-2009, 03:25 PM
For some reason the preg_replace is ignored :S
This is what I get:

Warning: file_get_contents(blah my name is jazz!!!.php) [function.file-get-contents]: failed to open stream: No such file or directory in G:\testing.php on line 4
[]

when it should just be "blah.php"

oesxyl
10-24-2009, 04:00 PM
For some reason the preg_replace is ignored :S
This is what I get:

Warning: file_get_contents(blah my name is jazz!!!.php) [function.file-get-contents: failed to open stream: No such file or directory in G:\testing.php on line 4
[]

when it should just be "blah.php"
wrong filename?, wrong place?

best regards

Jazz914
10-24-2009, 04:35 PM
its the wrong file name because the preg_replace is being ignored...


$blah = "{blah} my name is Jazz!!!";
$filename = preg_replace("/\{([^\}]+)\}/", "$1", $blah);
$filecontent = file_get_contents($filename . ".php");
echo "[" . $filecontent . "]"; // ???

the code above doesn't look for "blah.php" it looks for the whole quote, "blah my name is Jazz!!!.php"

oesxyl
10-24-2009, 05:40 PM
its the wrong file name because the preg_replace is being ignored...


$blah = "{blah} my name is Jazz!!!";
$filename = preg_replace("/\{([^\}]+)\}/", "$1", $blah);
$filecontent = file_get_contents($filename . ".php");
echo "[" . $filecontent . "]"; // ???

the code above doesn't look for "blah.php" it looks for the whole quote, "blah my name is Jazz!!!.php"


$blah = "{blah} my name is Jazz!!!";
$filename = preg_replace("/\{([^\}]+)\}/", "$1", $blah);
print $filename; // <--- insert this and test
$filecontent = file_get_contents($filename . ".php");
echo "[" . $filecontent . "]"; // ???

best regards

Jazz914
10-24-2009, 10:53 PM
Sorry forgot to mention I tried that, the result was

"blah my name is Jazz!!!" when it should just be "blah"

oesxyl
10-25-2009, 10:20 AM
Sorry forgot to mention I tried that, the result was

"blah my name is Jazz!!!" when it should just be "blah"
yes, is my fault, I didn't test it.



<?php

$blah = "{blah} my name is Jazz!!!";
$filename = preg_replace("/\{([^\}]+)\}.*/", "\\1", $blah);
print $filename; // <--- insert this and test
if(file_exists($filename)){
$filecontent = file_get_contents($filename . ".php");
echo "[" . $filecontent . "]"; // ???
}
?>

adding a .* after the match part will remove the rest of the text.

best regards

Jazz914
10-25-2009, 03:22 PM
I tried it and the format of the word is correct but for some reason it won't find the file
I added this to the end of the code for debugging purposes (as it actually states something)

}else{
echo("File doesn't exist");
}
but echoing out the quote it echos out the word "blah" and not the whole statement which is good but confusing as when you add the .php to it, it is "blah.php" which is correct :S
------------------------------------
EDIT:
Just noticed that you have the file check before actually adding the .php so its actually just looking for the file "blah" instead of "blah.php" :P Works now, thanks ^^

One more thing, if I wanted multiple of these would I loop through it using a for loop?

By multiple I mean, having something like this: "{Blah}, {Blah2}, {Blah3}" and is it possible to not erase the rest of the quote so it turns out like this:

{FirstFileContent} content in main file {SecondFileContent} More content in main file {ThirdFileContent} and on...

oesxyl
10-25-2009, 03:54 PM
I tried it and the format of the word is correct but for some reason it won't find the file
I added this to the end of the code for debugging purposes (as it actually states something)

}else{
echo("File doesn't exist");
}
but echoing out the quote it echos out the word "blah" and not the whole statement which is good but confusing as when you add the .php to it, it is "blah.php" which is correct :S
------------------------------------
EDIT:
Just noticed that you have the file check before actually adding the .php so its actually just looking for the file "blah" instead of "blah.php" :P Works now, thanks ^^

the reason I put there was to change output in case of error( not implemented but that was the intention).



One more thing, if I wanted multiple of these would I loop through it using a for loop?
it is possible but you also can use preg_match or preg_filter instead of preg_replace.


By multiple I mean, having something like this: "{Blah}, {Blah2}, {Blah3}" and is it possible to not erase the rest of the quote so it turns out like this:

{FirstFileContent} content in main file {SecondFileContent} More content in main file {ThirdFileContent} and on...
a note about preg_replace. If you have:


$a = preg_replace($regex, $prelace, $b);

after this line $b will be same as before.
Back to your problem, I would use preg_match and extract a array with all filenames, then check in a loop if each exists and do whatever you want to do with content.
Take care to filenames and path, are different between windows and linux, and try to use absolute path, more exactly relative to the root of the website( if is a site where you use this).

best regards

Jazz914
10-25-2009, 04:49 PM
Thanks, I was thinking something a long them lines but when I started to do it, this is what I started with

$blah = "{blah} my name is Jazz!!! {file}";
$filename = preg_match("/\{([^\}]+)\}.*/", "\\1", $blah);

echo count($filename);
I added the count(); to test if the preg_math would collect the similar values but it returns 1 and not 2?

oesxyl
10-25-2009, 05:10 PM
Thanks, I was thinking something a long them lines but when I started to do it, this is what I started with

$blah = "{blah} my name is Jazz!!! {file}";
$filename = preg_match("/\{([^\}]+)\}.*/", "\\1", $blah);

echo count($filename);
I added the count(); to test if the preg_math would collect the similar values but it returns 1 and not 2?
from manual:


Counts all elements in an array, or properties in an object.


http://www.php.net/manual/en/function.count.php

also for preg_match:




Return Values

preg_match() returns the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match. preg_match_all() on the contrary will continue until it reaches the end of subject . preg_match() returns FALSE if an error occurred.

http://www.php.net/manual/en/function.preg-match.php

is my fault, I suggested you to use preg_match when you probably need preg_match_all.

http://www.php.net/manual/en/function.preg-match-all.php

best regards

Jazz914
10-25-2009, 05:24 PM
I tried preg_match_all and my result was this:
I first counted and the result was again, 1 so I then I print_r() the array and got the following:


Array ( [0] => Array ( ) [1] => Array ( ) ) File doesn't exist#

Why doesn't it recognise there's more than one value? :S

oesxyl
10-25-2009, 05:29 PM
I tried preg_match_all and my result was this:
I first counted and the result was again, 1 so I then I print_r() the array and got the following:

#

Why doesn't it recognise there's more than one value? :S
.* eat the rest after first match, you must change the regex.
preg_match regex is a litte different then preg_replace regex.
I would try

[^\{]*
instead of .*

best regards

Jazz914
10-25-2009, 09:05 PM
Thanks the count works now ^^, if I have any more trouble I'll repost ^_^

Jazz914
10-25-2009, 09:46 PM
Okay, I tried adding a foreach loop, didn't work, this is what I tried:


<?php
$blah = "{blah} my name is Jazz!!! {file}";
$filename = preg_match_all("/\{([^\}]+)\}[^\{]*/", "\\1", $blah);
print_r("<br />".count($blah)."<br /><br />");
foreach($blah as $val) {
print_r("<br />".$val."<br /><br />");
if(file_exists($val.".php")){
$filecontent = file_get_contents($val.".php");
echo "[" . $filecontent . "]"; // ???
}else{
echo("<br />File doesn't exist<br /><br />");
}
}
?>

My error was the following:

Firstly, it says that the two values "blah" holds is "[1]Array and [2]Array" even with print_r

Secondly, because of the above problem I think its searching for a file of the name "array.php" so I added the file "Array.php" to the file browser and as expected it included the file "Array.php" with no problems.


EDIT:
The loads of breaks is for debugging purposes only :P (makes it easier to distinguish whats from what)

Also: the rest of the quote is not being displayed?

$blah = "{blah} my name is Jazz!!! {file}"; is the quote but the "my name is Jazz!!!" is being removed?

oesxyl
10-25-2009, 09:56 PM
do you use in a wrong way preg_match_all:



$howmany = preg_match_all("/\{([^\}]+)\}[^\{]*/", "\\1", $blah, $filenames);


read the manual for preg_match_all, $filenames will store matches in a specific way and you must extract them, again, from there.

best regards

Jazz914
10-25-2009, 10:18 PM
Okay I checked out the manual and wouldn't yours be wrong then, and the following be right?

$howmany = preg_match_all("/\{([^\}]+)\}[^\{]*/", $blah, $matches);(Removed the "\\1") also, why is it when I try to loop through this I get an error on the different things I try for example:



1: Foreach loop on $blah

foreach($blah as $val) { returns:

Warning: Invalid argument supplied for foreach() in G:\testing.php on line 5

2: Foreach loop on $howmany

foreach($howmany as $val) { returns:

Warning: Invalid argument supplied for foreach() in G:\testing.php on line 5
3: Foreach loop on $matches
The same Problem I was having on THIS (http://codingforums.com/showpost.php?p=880620&postcount=21) post

oesxyl
10-25-2009, 10:24 PM
Okay I checked out the manual and wouldn't yours be wrong then, and the following be right?

$howmany = preg_match_all("/\{([^\}]+)\}[^\{]*/", $blah, $matches);(Removed the "\\1") also, why is it when I try to loop through this I get an error on the different things I try for example:
because $blah and $howmany are not arrays, first is your string and second is a number. The results you want are in $filenames.
print_r $filenames to see what you get and to know how to extract file names from there.

best regards

Jazz914
10-25-2009, 11:33 PM
I tried print_r file names and I get the following:

Array ([0]=>Array() [1]=>Array() [2]=>Array())
and the array values should be occupied by the matched values but they are not :S

oesxyl
10-25-2009, 11:47 PM
I tried print_r file names and I get the following:

and the array values should be occupied by the matched values but they are not :S


<?php
$blah = "{blah} my name is Jazz!!! {file}";
$howmany = preg_match_all("/\{([^\}]+)\}[^\{]*/", $blah, $filenames);
print_r($filenames);
/* Output is:
Array
(
[0] => Array
(
[0] => {blah} my name is Jazz!!!
[1] => {file}
)

[1] => Array
(
[0] => blah
[1] => file
)

)
*/
foreach($filenames[1] as $file){
echo $file."<br/>";
}
?>


best regards

Jazz914
10-26-2009, 12:43 PM
Thank you very much oesxyl :D

Jazz914
10-26-2009, 05:43 PM
Okay if I wanted to change the regex to look for something like [] or !! instead of {}
Would I do this for the preg_match_all "/\[([^\}]+)\][^\[]*/" ([])?
or preg_match_all "/\!([^\!]+)\![^\!]*/" (!!)?

Sorry just trying to understand the regex a little better :)

oesxyl
10-26-2009, 06:14 PM
Okay if I wanted to change the regex to look for something like [] or !! instead of {}
Would I do this for the preg_match_all "/\[([^\}]+)\][^\[]*/" ([])?
or preg_match_all "/\!([^\!]+)\![^\!]*/" (!!)?

Sorry just trying to understand the regex a little better :)
yes, I guess you are right, :). Did you test it? it work as you want?

(, ), [, ], {, } have special meanings in a regex, they are metachars:
- (something), is a group, could be used as backreference, first is $1 or \\1, second $2 or \\2 and so on until $9
- [something] is a list of elements, "one of" something, [ab] means a or b. This could be used as [^ab] which means "not one of", ^ is not
- {n1}, {n1,n2}, {,n2} means that the expresion before this must match "exactly n1" times in first case, "more then n1 and less then n2" times in second case and "up to n2" in the last case
because of their special meaning sometimes if you want to use them as chars you must escape them using \. Not always escaping is necessary but because are many types of regex, I made a habit to do this to be sure, :)

best regards

Jazz914
10-26-2009, 11:07 PM
Thanks :thumbsup: I still notice that the rest of the file is being erased though,
Like:

Some information
{blah}
More information
{sec}

should translate to:

Some information
{file content from blah.php}
More information
{file content from sec.php}

but its turning out like this:

{file content from blah.php}
{file content from sec.php}

Is it possible to do what I want :S

oesxyl
10-26-2009, 11:21 PM
Thanks :thumbsup: I still notice that the rest of the file is being erased though,
Like:


should translate to:


but its turning out like this:


Is it possible to do what I want :S
see if you can use some flags. For example:


<?php

$text = "Some information
{blah}
More information
{sec} ";

$howmany = preg_match_all("/([^\{]*)\{([^\}]+)\}([^\{]*)/",$text,$results,PREG_OFFSET_CAPTURE);
print_r($results);


?>


best regards

Jazz914
10-27-2009, 02:50 PM
No difference :S

oesxyl
10-27-2009, 07:00 PM
No difference :S
what do you mean?
copy and past the code from my previous post and test it.

best regards

Phil Jackson
10-27-2009, 07:28 PM
I keep trying to get into this post but not understanding where abouts you are. What are you trying to do?

Phil Jackson
10-27-2009, 07:34 PM
does this help:



<?php

$text = "Some information
{blah}
More information
{sec} ";

$howmany = preg_match_all("#\{((?!\}).)*#is", $text, $results);
foreach($results[0] as $foo)
{
echo substr($foo, 1)."<br />";
}

//output
//blah
//sec
?>

oesxyl
10-27-2009, 07:39 PM
I keep trying to get into this post but not understanding where abouts you are. What are you trying to do?

to make a idea read this two posts:

http://www.codingforums.com/showpost.php?p=880448&postcount=13
http://www.codingforums.com/showpost.php?p=881018&postcount=30

ofcourse reading all thread will be better, :)

best regards

Phil Jackson
10-27-2009, 08:22 PM
<?php

$text = "Some information
{blah}
More information
{sec} ";

if(preg_match_all("#\{((?!\}).)*\}#is", $text, $results))
{
foreach($results[0] as $match)
{
$quote = preg_quote(substr($match, 1, -1), "#");
$contents = file_get_contents(substr($match, 1, -1).".php");
$text = preg_replace("#".$quote."#is", $contents, $text, 1);
}
}


?>

Phil Jackson
10-27-2009, 08:23 PM
because i have not got thos files i get result:



Warning: file_get_contents(blah.php) [function.file-get-contents]: failed to open stream: No such file or directory in C:\wamp\www\help\1.php on line 13

Warning: file_get_contents(sec.php) [function.file-get-contents]: failed to open stream: No such file or directory in C:\wamp\www\help\1.php on line 13

Phil Jackson
10-27-2009, 08:28 PM
slight alteration



<?php

$text = "Some information
{blah}
More information
{sec} ";

if(preg_match_all("#\{((?!\}).)*\}#is", $text, $results))
{
foreach($results[0] as $match)
{
$quote = preg_quote($match, "#");
$contents = file_get_contents(substr($match, 1, -1).".php");
$text = preg_replace("#".$quote."#is", $contents, $text, 1);
}
}

echo $text;

?>


blah.php:
this is file blah
sec.php:
this is file sec

output:

Some information this is file blah More information this is file sec

oesxyl
10-27-2009, 09:03 PM
<?php

$text = "Some information
{blah}
More information
{sec} ";

if(preg_match_all("#\{((?!\}).)*\}#is", $text, $results))
{
foreach($results[0] as $match)
{
$quote = preg_quote($match, "#");
$filename = substr($match, 1, -1).".php";
if(file_exists($filename)){
$contents = file_get_contents($filename);
}else{
$contents = "file " . $filename . " not exists or it's content is not available";
}
$text = preg_replace("#".$quote."#is", $contents, $text, 1);
}
}

echo $text;

?>

don't assume that everything is ok!!!!

best regards

Phil Jackson
10-27-2009, 09:20 PM
tu che clever clogs :-)

oesxyl
10-27-2009, 09:41 PM
tu che clever clogs :-)
:) seems you like to write complex code, :)
I was thinking if it is possible to use array_map and result of preg_match_all( the array), to do the same thing.:)

see my post where I said about using flags in preg_match_all
best regards

Phil Jackson
10-27-2009, 10:07 PM
ya'll pretty much had it, too much thought was put into the regex, and the key to it all, using preg_replace and limiting to 1 (at the end of the function). I like and can do more complex code better than simple code. I over think things, lol.

Jazz914
10-27-2009, 10:41 PM
Thank you very much :D One more question xD
I've noticed that the regex has changed, what does this change improve as I thought that the previous regex was fine?

Phil Jackson
10-28-2009, 12:46 AM
The regex was just lengthy. returns {blah} and {sec} using regex's lookaround.
wanting {, followed by anything except, } followed by }

\{[^\}]*\} could also be used but my way is more versatile if wanting to change the brackets such as

[||||||]blah[||||||]

would be

#\[\|\|\|\|\|\|\]((?!\[\|\|\|\|\|\|\]).)*\[\|\|\|\|\|\|\]#is

Jazz914
10-28-2009, 12:34 PM
thanks again, regex is confusing xD I'll have to spend some time learning it, do you have any good sources?

oesxyl
10-28-2009, 02:41 PM
thanks again, regex is confusing xD I'll have to spend some time learning it, do you have any good sources?
that depend on your skill. For example somebody who feel confortable with math will need another kind of explanation then usuals.
Try this and let me know if is what you need:

http://immike.net/blog/2007/04/06/the-absolute-bare-minimum-every-programmer-should-know-about-regular-expressions/
http://immike.net/blog/2007/06/21/extreme-regex-foo-what-you-need-to-know-to-become-a-regular-expression-pro/

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum