Topic: "PHP regex question" (page 1 of 1)

1
Author Post
velo
groupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmaster
Hi all,
I need a little help about creating regex wit this piece of code. I need cut all the code except bolded text (without br tag) which should be output of PHP script. So, I need a text inside pre tag, but without br tag. Does anybody knows how to do that?

<html>
<head>
<title>Title</title>
<link href="/css/some.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="some.js"></script>
</head>
<body>
<div class="some text">
<div class="title"> Some Title </div>
<h3>Subtitle</h3>
<p>Some text, another text, etc..</p>

<p>Some another text.</p>
I need only these numbers (in php array, if it is possible)---><pre>86,45,20,26,37,18,2,82,33,25,59,90,73,26,28,85,87,75,20,79,14,72,59,63,32,<br />72,62,12,46,29,39,36,61,45,7,72,54,14,86,47,33,90,23,72,78,43,98,77,99,32,<br />95,95,91,0,72,47,65,79,51,31,46,20,93,0,79,91,20,59,47,52,51,47,63,60,0,<br />68,28,22,67,62,63,43,10,77,13,29,31,27,8,52,84,57,81,85,78,27,80,16,51,52,<br />44,73,69,42,61,54,57,39,41,69,53,34,28,17,30,21,30,58,67,3,65,36,95,25,6,<br />34,83,19,80,47,32,22,95,13,81,43,30,34,95,3,39,68,89,53,83,6,76,51,44,76,<br />73,74,79,26,37,2,44,12,94,99,19,65,98,28,95,93,43,17,20,95,6,63,57,59,6,<br />90,14,88,28,19,70,1,12,48,73,84,74,37,42,36,32,93,42,67,13,7,19,19,72,79,<br />39,33,17,56,24,34,56,45,8,91,89,27,30,22,17,74,52,8,71,41,9,86,11,78,31,<br />48,79,68,56,31,27,76,54,92,91,58,36,89,31,12,16,5,79,77,55,75,2,48,72,3,<br />42,36,13,28,24,34,37,42,31,77,79,43,88,42,1,83,88,40,8,83,64,65,54,94,18,<br />82,35,54,61,92,84,5,46,56,64,71,49,69,65,36,86,30,57,60,41,99,97,35,66,96,<br />71,52,4,65,7,70,3,52,38,95,5,52,86,48,17,66,28,83,58,66,21,46,24,50,42,<br />72,34,67,83,41,33,9,4,12,34,77,19,26,8,65,29,42,11,44,82,8,51,30,12,18,<br />83,1,79,0,64,26,1,32,24,16,72,25,17,21,47,81,69,99,34,98,79,39,74,65,11,<br />26,59,42,77,63,45,49,15,39,5,77,64,86,54,30,26,48,93,28,38,66,96,29,10,87,<br />82,22,49,88,4,19,86,11,96,45,66,89,60,41,73,42,71,34,33,63,81,2,3,68,2,<br />23,93,59,80,50,47,52,3,56,33,74,98,54,45,99,14,89,47,40,35,17,36,12,32,60,<br />24,51,49,17,7,18,15,40,40,59,89,23,34,0,12,57,86,76,9,87,68,11,72,79,76,<br />6,87,38,3,36,71,34,82,84,90,14,27,30,29,83,16,71,51,31,49,59,95,32,84,84<br /></pre>

<div id="show"><<a href="#" onMouseOver="toggleDiv('explain',1)" onMouseOut="toggleDiv('explain',0)">explain</a>></div>
<div id="explain"><p>Explaination</p>
<p>blablabla</p>
<p>bla bla</p></div>
</div>
</body>
</html>
private message
sniperkid
groupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmaster
If there is only one place where it says <pre>text</pre> then you could try and use this:

<?
$html = "<html>
<head>
<title>Title</title>
<link href='/css/some.css' rel='stylesheet' type='text/css' />
<script type='text/javascript' src='some.js'></script>
</head>
<body>
<div class='some text'>
<div class='title'> Some Title </div>
<h3>Subtitle</h3>
<p>Some text, another text, etc..</p>

<p>Some another text.</p>
I need only these numbers (in php array, if it is possible)---><pre>86,45,20,26,37,18,2,82,33,25,59,90,73,26,28,85,87,75,20,79,14,72,59,63,32,<br />72,62,12,46,29,39,36,61,45,7,72,54,14,86,47,33,90,23,72,78,43,98,77,99,32,<br />95,95,91,0,72,47,65,79,51,31,46,20,93,0,79,91,20,59,47,52,51,47,63,60,0,<br />68,28,22,67,62,63,43,10,77,13,29,31,27,8,52,84,57,81,85,78,27,80,16,51,52,<br />44,73,69,42,61,54,57,39,41,69,53,34,28,17,30,21,30,58,67,3,65,36,95,25,6,<br />34,83,19,80,47,32,22,95,13,81,43,30,34,95,3,39,68,89,53,83,6,76,51,44,76,<br />73,74,79,26,37,2,44,12,94,99,19,65,98,28,95,93,43,17,20,95,6,63,57,59,6,<br />90,14,88,28,19,70,1,12,48,73,84,74,37,42,36,32,93,42,67,13,7,19,19,72,79,<br />39,33,17,56,24,34,56,45,8,91,89,27,30,22,17,74,52,8,71,41,9,86,11,78,31,<br />48,79,68,56,31,27,76,54,92,91,58,36,89,31,12,16,5,79,77,55,75,2,48,72,3,<br />42,36,13,28,24,34,37,42,31,77,79,43,88,42,1,83,88,40,8,83,64,65,54,94,18,<br />82,35,54,61,92,84,5,46,56,64,71,49,69,65,36,86,30,57,60,41,99,97,35,66,96,<br />71,52,4,65,7,70,3,52,38,95,5,52,86,48,17,66,28,83,58,66,21,46,24,50,42,<br />72,34,67,83,41,33,9,4,12,34,77,19,26,8,65,29,42,11,44,82,8,51,30,12,18,<br />83,1,79,0,64,26,1,32,24,16,72,25,17,21,47,81,69,99,34,98,79,39,74,65,11,<br />26,59,42,77,63,45,49,15,39,5,77,64,86,54,30,26,48,93,28,38,66,96,29,10,87,<br />82,22,49,88,4,19,86,11,96,45,66,89,60,41,73,42,71,34,33,63,81,2,3,68,2,<br />23,93,59,80,50,47,52,3,56,33,74,98,54,45,99,14,89,47,40,35,17,36,12,32,60,<br />24,51,49,17,7,18,15,40,40,59,89,23,34,0,12,57,86,76,9,87,68,11,72,79,76,<br />6,87,38,3,36,71,34,82,84,90,14,27,30,29,83,16,71,51,31,49,59,95,32,84,84<br /></pre>

<div id='show'><<a href='#' onMouseOver='toggleDiv('explain',1)' onMouseOut='toggleDiv('explain',0)'>explain</a>></div>
<div id='explain'><p>Explaination</p>
<p>blablabla</p>
<p>bla bla</p></div>
</div>
</body>
</html>";

$text = split('<pre>', $html);

// $text[1] should have <pre> -> EOF

$numbers = split('</pre>', $text[1]);

// $numbers[0] should be now the numbers you want //

$numbers = str_replace("<br />", "", $numbers);

// final var is $numbers[0] //

echo $numbers[0];
?>


My PHP is a little rusty so i may have somethings a little mixed up or wrong way round, but it should be near enough correct :).

Edit: Just tested it and it seems to be working fine, the only problem i had was it kept giving me an error because when i defining the $html var there was mixed " and ' in the html code. The script i posted above does work and displays the numbers as 1 string.

Edited by sniperkid on 09.01.2008 16:24:14
private message Website
unknown user
i'm no php-wiz but (there is always a but) I believe it's something like this

ereg("<pre>(.*)</pre>",$input,$hits);

echo $hits[1];

but i strongly recommend you
- test that
- are aware that under some circumstances evaluating regular expressions, can lead to code execution :P
EMail
velo
groupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmaster
Thanks guys,
Another reply?
private message
Towley
groupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmaster
I suggest you this pseudo-code:

1. Take the text between the <pre> tags.
2. Replace the <br> tags with empty strings. (you could also eleminate _all_ html tags with a regex)
3. Replace the commas(,) with a space.
4. Loop over the text, scan one number at a time and store it in the array, loop until you reach the end of text.

Step 4 can be done in many ways (especially in PHP),
Here is an example that _might_ work. Didn't try it out and i didn't code php for some time.
Also this is a bad solution, because it is very slow.

QuoteQuote from Scratch:

$nums = array();
while ($text != "") { // as long as there is text left:
$nums[] = (int) substr($text, 0, strpos($text, " "); // scan a number and append it to array.
$text = substr($text, strpos($text, " "); // new text is the the text after first space.
}

private message Website
clyto
groupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmaster
this may help you
preg_match_all("/(\d+)\D+/",$input,$output);
var_dump($output);

ciao ;-)
private message
quangntenemy
groupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmaster
Maybe you should not spoil challenges in other sites here...
private message EMail Website
velo
groupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmaster
Thanks again guys!
@quangntenemy: "...302 challs solved, too 1337 to be ranked :)..." Maybe it is an only reason...? :P btw, it is not spoiling, it is only pure regex question. Don't afraid, you're untouchable there...:D
private message

Topic: "PHP regex question" (page 1 of 1)

1