add CSS with array of variables in head - Wordpress - PHP - php

I'm trying to return css build with multiple variables in my Wordpress website, its kind of working but it's only returning the first array. If i echo instead of return inside the foreach it shows the code correctly but outside the correct location.
Sorry for the bad code and possible definitions about php. I have only just started to learn php.
The code
function child_custom_color_css4() {
function child_custom_color_css2() {
function test_loop_1_beta() {
$boo = 'soortverhaal0 ';
$foo = 'soortverhaal1 ';
return array($boo, $foo);
}
$loops = test_loop_1_beta();
global $nectar_options;
$Styled=array();
foreach ($loops as $loop) {
return '
#test-loop.'. $loop .' {
background-color: '.esc_attr($nectar_options["accent-color"]).' !important;
}
';
}
return implode($Styled);
}
$get_arrays = child_custom_color_css2();
wp_add_inline_style( 'main-styles', $get_arrays);
}
add_action('wp_enqueue_scripts','child_custom_color_css4', 20); '''
Output:
<style id="main-styles-inline-css" type="text/css">
#test-loop.soortverhaal0 {
background-color: #f04e23 !important;
}
</style>
Edit: what need:
<style id="main-styles-inline-css" type="text/css">
#test-loop.soortverhaal0 {
background-color: #f04e23 !important;
}
#test-loop.soortverhaal1 {
background-color: #f04e23 !important;
}
</style>

I think you could do something like that:
function child_custom_color_css4() {
function child_custom_color_css2() {
$loops = [
'soortverhaal0 ',
'soortverhaal0 ',
];
global $nectar_options;
$styled = [];
foreach ($loops as $loop) {
$styled[] = '#test-loop.' . $loop . ' { background-color: ' . esc_attr( $nectar_options['accent-color'] ) . ' !important; }';
};
return implode("\n",$styled);
}
$get_arrays = child_custom_color_css2();
wp_add_inline_style( 'main-styles', $get_arrays);
}
add_action('wp_enqueue_scripts','child_custom_color_css4', 20);
I do an array push to acumulate each loop into $style array.
I think you would need to define other separator for the implode function like:
return implode("\n",$styled)

Related

How to add tr:nth-child inside tag table?

Content my controller :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class AutoLoadDiv extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('ngoding/AutoLoad');
}
public function getData() {
$this->load->library('table');
$err = file_get_contents("application\logs\log.php");
$filestr = preg_replace(array('/(^|\R)ERROR\s*-\s*/', '/(^|\R)(.*?)\s*-->\s*/'), array('$1', '$1$2 '), $err);
$arraySpacing = nl2br($filestr);
$arr = explode("\n", $arraySpacing);
$output = '<table style="tr:nth-child(even){background-color#FFF;} tr:nth-child(odd){background-color:#CCC;}", border="1px solid #dddddd;" >';
for ($i = count($arr)-1; $i >= 0; $i--) {
$output.="<tr><td>$arr[$i]</td></tr>";
}
$output.="</table>";
echo $output;
}
}
I have problem to add color background every row is different row, I found my answer but not working , that answer is add code inside tr:nth-child(even) and tr:nth-child(odd) ,how to add tr:nth-child(even) and tr:nth-child(odd) in tag ?
Prepend another $output that contains the stylesheet, that is like :
$output = '<style> tr:nth-child(even){background-color#FFF;} tr:nth-child(odd){background-color:#CCC;} </style>';
$output .= '<table border="1px solid #dddddd;" >';

php regex select less code

I want to select all less code from a file .But i can not find the right way to do it.
This is my code so far
$d = <<<EOT
.class {
width:100%;
height:100%;
background-image: url(images/fallback-gradient.png);
}
.id {
b:100%;
bc: url(images/fallback-gradient.png);
}
& when ( #main_container_top_option = true) {
.fast_menu_option {
.gpicon {
color: transparent;
}
}
}
EOT;
$pattern = '/.*\{(?s:.*?)\}$/mi';
$t = preg_match_all($pattern, $d, $out, PREG_PATTERN_ORDER);
foreach ($out[0] as $key) {
echo '<br><pre>'.$key."</pre>";
}
And this is the result:
.class {
width:100%;
height:100%;
background-image: url(images/fallback-gradient.png);
}
.id {
b:100%;
bc: url(images/fallback-gradient.png);
}
& when ( #main_container_top_option = true) {
.fast_menu_option {
.gpicon {
color: transparent;
}
For the first 2 classes is ok but last one is not ok because they are missing 2 other }.
How can i fix it?
It looks like you want to match individual blocks, so a recursive pattern will return balanced braces. As stated in comments under the question, this method may break when character anomalies occur. I believe there are css parsers out "in the wild" but I don't have any to recommend.
Code: (Demo)
if (preg_match_all('~.*\{((?R)|[^}])*}~', $d, $out)) {
echo implode("\n---\n",$out[0]);
}
Output:
.class {
width:100%;
height:100%;
background-image: url(images/fallback-gradient.png);
}
---
.id {
b:100%;
bc: url(images/fallback-gradient.png);
}
---
& when ( #main_container_top_option = true) {
.fast_menu_option {
.gpicon {
color: transparent;
}
}
}
If you don't need to separate the blocks, you can modify the pattern like this:
~[^{]*\{((?R)|[^}])*}~
But then it depends on what your actual input.

How to remove a line from text file using php with specific structure [duplicate]

This question already has answers here:
How to delete a line from the file with php?
(10 answers)
Closed last year.
I have a css file with this content:
#firstdiv { width:100%; height:200px; }
#seconddiv { width:80%; height:70px; }
#thirddiv { width:80%; height:70px; }
#firstdiv { color:red; background:yellow; }
#seconddiv { color:red; background:green; }
#firstdiv { border:3px solid black; border-rdius:5px; }
How can I remove all #firstdiv css properties using php?
This is my desired output:
#seconddiv { width:80%; height:70px; }
#thirddiv { width:80%; height:70px; }
#seconddiv { color:red; background:green; }
The most easy way would to split the file based on newlines and then check for each row whether it starts with the string you don't want and finally save the file to the location.
$filelocation = "/path/to/file"; //please update for your situation
$csscontents = file_get_contents($filelocation);
$lines = explode(PHP_EOL,$csscontents);
$csscontents = '';
foreach($lines as $line) {
if (substr($line,0,9) !== "#firstdiv") $csscontents .= $line . PHP_EOL;
}
file_put_contents($filelocation,$csscontents);
In case there are multiple selectors on one line, you need to do this
$filelocation = "/path/to/file"; //please update for your situation
$csscontents = file_get_contents($filelocation);
$lines = explode('}',$csscontents);
$csscontents = '';
foreach($lines as $line) {
if (substr(preg_replace('/\s+/', '',$line),0,9) !== "#firstdiv" AND !empty($line) AND !ctype_space($line)) $csscontents .= $line . "}";
}
file_put_contents($filelocation,$csscontents);

How to loop trought array and create array from common values?

i need to filter an array and spilt them into different arrays. Here the actual working and slow code:
-----> http://viper-7.com/GVRbVp
but that method is still slow, i think. At least 3 loops...
i would like to scan one time only the array and create array on the fly, i tried with this:
$stray = json_decode('[{"id":"1","zona":"Pescara"},{"id":"2","zona":"Pescara"},{"id":"3","zona":"Teramo"},{"id":"4","zona":"Pescara"},{"id":"5","zona":"Pescara"},{"id":"6","zona":"Teramo"},{"id":"7","zona":"Pescara"},{"id":"8","zona":"Pescara"},{"id":"9","zona":"Pescara"},{"id":"10","zona":"Pescara"},{"id":"11","zona":"Pescara"},{"id":"12","zona":"Pescara"},{"id":"13","zona":"Teramo"},{"id":"14","zona":"Chieti"},{"id":"15","zona":"Chieti"},{"id":"16","zona":"Aquila"},{"id":"17","zona":"Chieti"},{"id":"18","zona":"Chieti"},{"id":"19","zona":"Chieti"},{"id":"20","zona":"Chieti"},{"id":"21","zona":"Campobasso"},{"id":"22","zona":"Aquila"},{"id":"23","zona":"Pescara"},{"id":"24","zona":"Pescara"},{"id":"25","zona":"Pescara"},{"id":"26","zona":"Pescara"},{"id":"27","zona":"Chieti"},{"id":"28","zona":"Pescara"},{"id":"29","zona":"Pescara"},{"id":"30","zona":"Chieti"},{"id":"31","zona":"Pescara"},{"id":"32","zona":"Chieti"},{"id":"33","zona":"Teramo"},{"id":"34","zona":"Teramo"},{"id":"35","zona":"Teramo"},{"id":"37","zona":"Teramo"},{"id":"39","zona":"Pescara"}]',true);
$all_cat = array();
foreach($stray as $row) {
$item_cat = $row['zona'];
if($$cat) { /* check if the category array exist */
$cat = array(); /* if not, create array */
if( !in_array($item_cat,$$item_cat) ) { /* and add the value */
array_push($$item_cat,$row);
}
array_push($all_cat, $cat); /* add new category to index of categories */
} else {
if( !in_array($item_cat,$$item_cat) ) { /* Otherwise just add the value */
array_push($$item_cat,$row);
}
}
}
echo '<pre>'.print_r($cat,true).'</pre>';
If I understand correctly, you could simplify it to
foreach($stray as $row) {
if(!isset($$row['zona'])) {
$$row['zona']=array();
}
${$row['zona']}[] = $row;
}
so now your viper-7 would look like
$stray = json_decode(...[removed to simplify]...,true);
foreach($stray as $row) {
if(!isset($$row['zona'])) {
$$row['zona']=array();
}
${$row['zona']}[] = $row;
}
echo 'Pescara :<pre style="max-height: 50px; overflow: auto">'.print_r($Pescara,true).'</pre>';
echo 'Teramo :<pre style="max-height: 50px; overflow: auto">'.print_r($Teramo,true).'</pre>';
echo 'Chieti :<pre style="max-height: 50px; overflow: auto">'.print_r($Chieti,true).'</pre>';
echo 'Aquila :<pre style="max-height: 50px; overflow: auto">'.print_r($Aquila,true).'</pre>';
echo 'Campobasso :<pre style="max-height: 50px; overflow: auto">'.print_r($Campobasso,true).'</pre>';
For loop through the array. For every element do if else checking for all 3 "zona". If it matches one push it to the respective array within the if and move on to the next element in the array.

Facebook like link parser

Is there any already done php class that could parse a link like Facebook, Google+ or Digg does? To get the title, some text and images from the page? :)
Thanks
Here is some code I pinched from sitepoint.com. I have used it a few times and it seems to work nicely...
<?php
define( 'LINK_LIMIT', 30 );
define( 'LINK_FORMAT', '%s' );
function parse_links ( $m ){
$href = $name = html_entity_decode($m[0]);
if ( strpos( $href, '://' ) === false ) {
$href = 'http://' . $href;
}
if( strlen($name) > LINK_LIMIT ) {
$k = ( LINK_LIMIT - 3 ) >> 1;
$name = substr( $name, 0, $k ) . '...' . substr( $name, -$k );
}
return sprintf( LINK_FORMAT, htmlentities($href), htmlentities($name) );
}
$s = 'Here is a text - www.ellehauge.net - it has some links with e.g. comma, www.one.com,in it. Some links look like this: http://mail.google.com - mostly they end with aspace or carriage return www.unis.no<br /> - but they may also end with a period: http://ellehauge.net. You may even putthe links in brackets (www.skred-svalbard.no) (http://one.com).From time to time, links use a secure protocol like https://gmail.com |This.one.is.a.trick. Sub-domaines: http://test.ellehauge.net |www.test.ellehauge.net | Files: www.unis.no/photo.jpg |Vars: www.unis.no?one=1&~two=2 | No.: www.unis2_check.no/doc_under_score.php |www3.one.com | another tricky one:http://ellehauge.net/cv_by_id.php?id%5B%5D=105&id%5B%5D=6&id%5B%5D=100';
$reg = '~((?:https?://|www\d*\.)\S+[-\w+&##/%=\~|])~';
print preg_replace_callback( $reg, 'parse_links', $s );
?>
This looks like something you could use:
http://www.redsunsoft.com/2011/01/parse-link-like-facebook-with-jquery-and-php/
index.php
<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<style>body
{
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
}
#contentbox
{
width:458px; height:50px;
border:solid 2px #dedede;
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
margin-bottom:6px;
}
.img
{
float:left; width:150px; margin-right:10px;
text-align:center;
}
#linkbox
{
border:solid 2px #dedede; min-height:50px; padding:15px;
display:none;
}</style>
<script type="text/javascript">
$(document).ready(function()
{
$("#contentbox").keyup(function()
{
var content=$(this).val();
var urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|!:,.;]*[-A-Z0-9+&##\/%=~_|])/ig;
// Filtering URL from the content using regular expressions
var url= content.match(urlRegex);
if(url.length>0)
{
$("#linkbox").slideDown('show');
$("#linkbox").html("<img src='link_loader.gif'>");
// Getting cross domain data
$.get("urlget.php?url="+url,function(response)
{
// Loading <title></title>data
var title=(/<title>(.*?)<\/title>/m).exec(response)[1];
// Loading first .png image src='' data
var logo=(/src='(.*?).png'/m).exec(response)[1];
$("#linkbox").html("<img src='"+logo+".png' class='img'/><div><b>"+title+"</b><br/>"+url)
});
}
return false;
});
});
//HTML Code
<textarea id="contentbox"></textarea>
<div id="linkbox"></div>
</script>
urlget.php
<?php
if($_GET['url'])
{
$url=$_GET['url'];
echo file_get_contents($url);
}
?>
source

Categories