/    Sign up×
Community /Pin to ProfileBookmark

preg_match per instance

I’m having a bit of trouble getting an additional increment per
instance. Should I get another foreach loop in there? Or is there
a better way to get this adding while it’s coming across new
occurrences of $v in the string?

[code=php]
foreach( $tmp AS $k => $v ) {

$v = preg_replace(array(‘/{/’, ‘/}/’), array(“<a href='”.$url[$i++].”‘>r”, ‘</a>’), $v);

$payload .= “<div class=’ids’>”;
$payload .= “<h3>{$k}</h3>”;

$payload .= “<div class=’hide’>”;

$payload .= “<div class=’PanelBox’><div class=’infoPanel’ style=’display: block;’>”;
$payload .= “<div class=’span-19 prepend-top prepend-half brandDescription’>”;

$payload .= “<h2 style=’font-size:15px;’>{$v}</h2>”;
//code
}
[/code]

value it’s catching on is

[code] its when your high backs press {into your} calf’s while making it {impossible} to ride. used to explain the emphasis of style in a trick. [/code]

In theory (lol) it should replace each instance of { with a url, right?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiJul 30.2010 — I don't really understand your issue. What is the problem you are having?
Copy linkTweet thisAlerts:
@ehimeauthorJul 30.2010 — Cool lemme explain it a bit better.

So I have an array called tmp that I create, it has values that have

links inside them that haven't been added yet. They way that I know

a text link is that they are covered like {link}. I'm trying to replace

the { with <a href='$url' and } with </a>. I get the links from another

array called $url. Unfortunately if there is MORE than one link in a

string it seems to replace it with the last value of the $url.

[code=php]
<?php
$x = @simplexml_load_file('export.xml')
or die('File could not be loaded.');

function dump($var) { echo '<pre>'; var_dump($var); echo '</pre>'; }

$tmp = $url = array();
foreach( $x AS $xml ) {
$n = $m = NULL;
foreach( $xml AS $k => $v ) {

$v = htmlentities($v);
if ($k == 'Name') {$n = $v;}
if ($k == 'Description') {$m = $v;}
if ($k == 'URL') { if ($v == '') {$url[] = '#';} else {if (stristr($v, '|')) {$url = explode('|', $v);} else {$url[] = $v;}}}
}
// $m = explode('{', $m);
// $count = count($m);
// foreach ($m AS $k){echo $k;
// while ($count != 0)
// { $url[$count-1] = array_push($k,$k[$count]);
// }}

// dump($m);

$tmp[$n] = $m;
}

//$magento_skin = "{{skin url='images/brandPages/}}";

$i=0; foreach ($tmp AS $k => $v) {
$v = str_replace(array('{', '}'), array("<a href='".$url[$i++]."'>r", '</a>'), $v);
$tmp[$k] = $v;}

foreach( $tmp AS $k => $v ) {
$payload .= "<div class='ids'>";
$payload .= "<h3>{$k}</h3>";

$payload .= "<div class='hide'>";

$payload .= "<div class='PanelBox'><div class='infoPanel' style='display: block;'>";
$payload .= "<div class='span-19 prepend-top prepend-half brandDescription'>";

$payload .= "<h2 style='font-size:15px;'>{$v}</h2>";

$payload .= "</div><div class='span-18 prepend-half prepend-top brandLinks'>";
$payload .= "</div></div></div>";

$payload .="<div class='span-19 prepend-top'>";
$payload .= "<div class='span-19 panelControl'><h3 class='toggle'>";

$payload .= "<a href='#' class='open' style='display: none;'>Learn More";
$payload .= "<img class='panelArrows' alt='More' src='{$magento_skin}panelOpen.gif' /></a>";
$payload .= "<a href='#' class='close' style=''>Close Panel";
$payload .= "<img class='panelArrows' alt='Less' src='{$magento_skin}panelUp.gif' /></a>";
$payload .= "</h3></div></div></div>";

$payload .= "</div>";
}

//re-wrap
$payload = "<div style='margin:auto 19px;width:350px;'>{$payload}</div>";
$payload = "<div style='width: 750px; border:solid 1px; #000'>{$payload}</div>";

echo $payload;
?>[/code]



export.xml
[code=html]
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
<Group>
<Name>*****IN CALFDICK</Name>
<Description>its when your high backs press {into your} calf's while making it {impossible} to ride. </Description>
<URL>HTTP://ETC1.COM|HTTP://ETC2.COM</URL>
</Group>
<Group>
<Name>BONED OUT</Name>
<Description>used to explain the emphasis of style in a trick. if someone {"boned out a indy"} they would grab hard and create an emphasis of the maneuver such that his/her legs may appear extended or stretched to a maximum degree. or the style of the indy was more.</Description>
<URL>HTTP://ETC3.COM</URL>
</Group>
<Group>
<Name>FRONT</Name>
<Description>This is the ultimate description of euphoria. That last trip to the mountains was FRONT!!!!Dude, he pulled of a {FRONT airdog}. Checkout the FRONT chick on the lift. Origin- Cold Front brings the start of the season, abreviate and it's FRONT bro.</Description>
<URL></URL>
</Group>
<Group>
<Name>3, 5, 7, 9, 19, 12</Name>
<Description>Summaries of respectfully a 360, 540, 720, 900, 1080 or 1260 turn. Used for {aerial tricks}.</Description>
<URL></URL>
</Group>
<Group>
<Name>5-0 GRIND</Name>
<Description>A 5-0 grind is when you jump on a box or rail in a 50-50 and do a butter the rest of the way.</Description>
<URL></URL>
</Group>
<Group>
<Name>50/50</Name>
<Description>is when your board is facing straight and it can be on any rail, not just a {house rail/box}</Description>
<URL></URL>
</Group>
<Group>
<Name>AASI</Name>
<Description>Stands for the the American Association of Snowboard Instructors. AASI is a way for snowboard instructors to put forth a safe, and fun lesson in which students can learn and enjoy the sport nationwide.</Description>
<URL></URL>
</Group>
<Group>
<Name>ACCESSORY MAN</Name>
<Description>A snowboarder with every piece of snowboard gear imaginable. He has been known to use it all at the same time whether or not the circumstances call for it.</Description>
<URL></URL>
</Group>
<Group>
<Name>AIRDOG</Name>
<Description>A snowboarder who jumps most of the time and is most interested in airial tricks</Description>
<URL></URL>
</Group>
<Group>
<Name>ARTIC COUGAR</Name>
<Description>An old lady who goes for really young guys on the slops and in the lodge</Description>
<URL></URL>
</Group>
<Group>
<Name>B.C.O.C</Name>
<Description>Boarders Code Of Conduct, Can be used as part of "are you part of the BCOC?" or "thats not part of the BCOC"</Description>
<URL></URL>
</Group>
<Group>
<Name>BACKSIDE RODEO</Name>
<Description>Coming off a jump and turning your back down the hill, flipping 540 and landing fakie or rotating a total of 720 and landing regular.</Description>
<URL></URL>
</Group>
<Group>
<Name>BATTLESHIP</Name>
<Description>A rail in the park that kinks up, then flattens out, then kinks back down.</Description>
<URL></URL>
</Group>
<Group>
<Name>BETTY</Name>
<Description>A girl who can't ride (a poser who is a girl). Characteristics include: a Roxy snowboard, sideslipping through park, flirting, being loud and obnoxious, matching jacket, pants, hat, etc.</Description>
<URL>http://notlikeabove.com</URL>
</Group>
<Group>
<Name>BLINGERS</Name>
<Description>Refers to the people who buy all matching {equipment} regardeles of quality or performance and are on the hill not to snowboard but to be seen snowboarding.</Description>
<URL></URL>
</Group>
<Group>
<Name>BLUE BIRD</Name>
<Description>A beautiful day. e.g. "Cuttin' the pow pow on a blue bird day."</Description>
<URL></URL>
</Group>
<Group>
<Name>BOMB HOLE</Name>
<Description>A big hole in the landing of a {powder jump} from somebody bailing. "If you make a bomb hole on the landing of that jump, everybody will hate you.'</Description>
<URL></URL>
</Group>
</Root>
[/code]
Copy linkTweet thisAlerts:
@ehimeauthorAug 02.2010 — Happy Monday bump.

So I was thinking over the weekend that

the best method might be to explode and

implode it before it hits the $tmp array if

possible. I think it might save me from future

shenanigans later on. So I'm taking a deeper

look at the code right here that I've commented

out.

[code=php]
$tmp = $url = array();
foreach( $x AS $xml ) {
$n = $m = NULL;
foreach( $xml AS $k => $v ) {

$v = htmlentities($v);
if ($k == 'Name') {$n = $v;}
if ($k == 'Description') {$m = $v;}
if ($k == 'URL') { if ($v == '') {$url[] = '#';} else {if (stristr($v, '|')) {$url = explode('|', $v);} else {$url[] = $v;}}}
}
// $m = explode('{', $m);
// $count = count($m);
// foreach ($m AS $k){echo $k;
// while ($count != 0)
// { $url[$count-1] = array_push($k,$k[$count]);
// }}

// dump($m);

$tmp[$n] = $m;
}
[/code]


This is still open and unsolved! To reiterate the issue, I am trying to

replace multiple instances of { in an array with contents of an exploded

string. Thanks for the help guys.
Copy linkTweet thisAlerts:
@ehimeauthorAug 02.2010 — Solved

[code=php]
<?php
$x = @simplexml_load_file('export.xml')
or die('File could not be loaded.');

function dump($var) { echo '<pre>'; var_dump($var); echo '</pre>'; }
function splice(&$output, $one = array(), $two = array(), $same_size= false) {
if ($same_size) { if (count($one) != count($two)) return FALSE;
$number = count($one); } else { $number = min(count($one), count($two));
} $output = array();

for ($i = 0; $i < $number+1; $i++) {
$output[] = $one[$i];
$output[] = "<a href='".$two[$i]."'>";
}
return TRUE;
}

$tmp = $url = array();
foreach( $x AS $xml ) {
$n = $m = NULL;
foreach( $xml AS $k => $v ) {

$v = htmlentities($v);
if ($k == 'Name') {$n = $v;}
if ($k == 'Description') {$m = $v;}
if ($k == 'URL') {

if (stristr($v, '|')) { $v = explode('|', $v); $m = explode('{', $m);
splice($v, $m, $v); $v = implode('', $v);
$v = str_replace('}', '</a>', $v); $m = $v;}

if (!stristr($v, '|') && $v != '') {
$m = str_replace('{', "<a href='{$v}'>", $m);
$m = str_replace('}', '</a>', $m); }

if (!stristr($v, '|') && $v == '') {
$m = str_replace('{', "<a href='#'>", $m);
$m = str_replace('}', '</a>', $m); }

}
} $tmp[$n] = $m;
}

//$magento_skin = "{{skin url='images/brandPages/}}";

foreach( $tmp AS $k => $v ) {
$payload .= "<div class='ids'>";
$payload .= "<h3>{$k}</h3>";

$payload .= "<div class='hide'>";

$payload .= "<div class='PanelBox'><div class='infoPanel' style='display: block;'>";
$payload .= "<div class='span-19 prepend-top prepend-half brandDescription'>";

$payload .= "<h2 style='font-size:15px;'>{$v}</h2>";

$payload .= "</div><div class='span-18 prepend-half prepend-top brandLinks'>";
$payload .= "</div></div></div>";

$payload .="<div class='span-19 prepend-top'>";
$payload .= "<div class='span-19 panelControl'><h3 class='toggle'>";

$payload .= "<a href='#' class='open' style='display: none;'>Learn More";
$payload .= "<img class='panelArrows' alt='More' src='{$magento_skin}panelOpen.gif' /></a>";
$payload .= "<a href='#' class='close' style=''>Close Panel";
$payload .= "<img class='panelArrows' alt='Less' src='{$magento_skin}panelUp.gif' /></a>";
$payload .= "</h3></div></div></div>";

$payload .= "</div>";
}

//re-wrap
$payload = "<div style='margin:auto 19px;width:350px;'>{$payload}</div>";
$payload = "<div style='width: 750px; border:solid 1px; #000'>{$payload}</div>";

echo $payload;
?>
[/code]
×

Success!

Help @ehime spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...