$response var has a component called custom_test_name which looks like below:
[Test_Name]ad.no.check1.check2.check3
and here is the small PHP code:
<?php
echo "<class="."com.tests.".$response["custom_test_name"][1]."</class>";
?>
This prints the <class=com.tests.[</class>..check first character [ of custom_test_name and similarly echoing of ["custom_test_name"][2] prints T, [3] prints e....However, how to print/echo only the specifics in this case?. For eg. echoing just this ad.no.check1.check2.check3 and eliminating out that [Test_Name]. Is there a way we can specify the range/some other approach?
If custom_test_name is always going to begin with [Test_Name] you can remove it with something like
$trimmed = str_replace('[Test_Name]', '', $response->custom_test_name);
echo "<class="."com.tests." . $trimmed . "</class>";
If it's not always going to be like that but is going to start with [something], you can use something like
$trimmed = preg_replace("/(\[.*\])/", '', $response->custom_test_name);
echo "<class="."com.tests." . $trimmed . "</class>";
Related
I'm trying to get rid of php code in a file using regex. Some of the php is not well-formatted, so that there may be extra spaces and/or line breaks. As an example:
<?php require_once('some_sort_of_file.php');
?>
I've come up with the following regex which seems to work:
$initial_text = preg_replace('/\s+/', ' ', $initial_text );
$initial_text = preg_replace('/' . preg_quote('<?php') . '.*?' . preg_quote('?>') . '/', '', $initial_text);
but was wondering if there might be a way to just use 1 regex statement, in order to speed things up.
Thanks!
An even better way to do it: use the built-in tokenizer. Regexes have problems with parsing irregular languages like PHP. The tokenizer, on the other hand, parses PHP code just like PHP itself does.
Sample code:
// some dummy code to play with
$myhtml = '<html>
<body>foo bar
<?php echo "hello world"; ?>
baz
</body>
</html>';
// Our own little function to do the heavy lifting
function strip_php($text) {
// break the code into tokens
$tokens = token_get_all($text);
// loop over the tokens
foreach($tokens as $index => $token) {
// If the token is not an array (e.g., ';') or if it is not inline HTML, nuke it.
if(!is_array($token) || token_name($token[0]) !== 'T_INLINE_HTML') {
unset($tokens[$index]);
}
else { // otherwise, echo it or do whatever you want here
echo $token[1];
}
}
}
strip_php($myhtml);
Output:
<html>
<body>foo bar
baz
</body>
</html>
DEMO
you can put it as a single regex using the s modifier which will allow the dot to match newline chars too. I added the i modifier too to make it case-insensitive.. dunno if you care about that:
$initial_text = preg_replace('~<\?php.*?\?>~si', '', $initial_text );
I am a weird issue regarding my class property here
I have the following:
$this->tableData = '<table>';
$this->tableData .= $string;
echo $this->tableData => output <table>
I want to concatenate more string to my $this->tableData but it seems like nothing is added.
I know $string is not null and contains characters
Did I do something wrong here?
Thanks!
To see if your string is not null you should use var_dump() or print_r() functions.
Example:
$this->tableData = '<table>';
echo "Dumping tableData: " . var_dump($this->tableData);
$this->tableData .= $string;
echo "Dumping tableData 2: " . var_dump($this->tableData);
echo "Dumping string: " . var_dump($string);
That way you will see exactly what is going on.
Is your variable $string containing a HTML tag, something like <p></p> or else ?
This could be "hidden" if you print_r it inside a browser.
$oku = mysql_fetch_array($yaz);
echo $oku[0];
this prints my $oku[0] value for example hello world
But if I use it like:
echo "" . $oku[0] . "";
it shows the hello world in text form exactly right, but link goes to index.php?member=hello
It does not contain the text after the space. How can solve this issue?
Use the php rawurlencode-function to encode you string (which contains a space)
Just do:
printf(
'%s',
'index.php?member=' . rawurlencode($oku[0]),
$oku[0]
);
Ok I need to find out what is contained inside a PHP variable and I have it to do it visually, is there a function to display whatever that's contained in a string as it is?
For example :
$TEST = ' ' . "\n" . ' ';
if I use echo the output will be :
while i want it to be :
 \n 
is it possible? (I hope I was clear enough)
ty
You can use json_encode with htmlspecialchars:
$TEST = ' ' . "\n" . ' ';
echo json_encode(htmlspecialchars($TEST));
Note that json_encode has third agrument in PHP 5.4.
var_dump() should do the work for you?
Example:
echo "<pre>";
var_dump($variable);
echo "</pre>";
Use <pre> to keep the format structure, makes it alot easier to read.
Resources:
http://php.net/manual/en/function.var-dump.php
http://www.w3schools.com/tags/tag_pre.asp
Try print_r, var_dump or var_export functions, you'll find them very handy for this kind of needs!
http://www.php.net/manual/en/function.htmlspecialchars.php
or
http://www.php.net/manual/en/function.htmlentities.php
$TEST = ' ' . "\n" . ' ';
echo htmlspecialchars(str_replace('\n','\\n', $TEST), ENT_QUOTES);
or
$TEST = ' ' . "\n" . ' ';
echo htmlentities(str_replace('\n','\\n',$TEST), ENT_QUOTES);
You may have to encode the newlines manually. If you want to encode them as actual newlines you can use nl2br. Or string replace these characters with your preference. Update: as I have added to the code per request. String replace special characters you wish to see like newlines and tabs.
assuming you want it for the debugging purposes, let me suggest to use urlencode(). I am using it to make sure I don't miss any invisible character.
The output is not that clear but it works for me.
its a little bit hard to understand.
in the header.php i have this code:
<?
$ID = $link;
$url = downloadLink($ID);
?>
I get the ID with this Variable $link --> 12345678
and with $url i get the full link from the functions.php
in the functions.php i have this snippet
function downloadlink ($d_id)
{
$res = #get_url ('' . 'http://www.example.com/' . $d_id . '/go.html');
$re = explode ('<iframe', $res);
$re = explode ('src="', $re[1]);
$re = explode ('"', $re[1]);
$url = $re[0];
return $url;
}
and normally it prints the url out.. but, i cant understand the code..
It's written in kind of a strange way, but basically what downloadLink() does is this:
Download the HTML from http://www.example.com/<ID>/go.html
Take the HTML, and split it at every point where the string <iframe occurs.
Now take everything that came after the first <iframe in the HTML, and split it at every point where the string src=" appears.
Now take everything after the first src=" and split it at every point where " appears.
Return whatever was before the first ".
So it's a pretty poor way of doing it, but effectively it looks for the first occurence of this in the HTML code:
<iframe src="<something>"
And returns the <something>.
Edit: a different method, as requested in comment:
There's not really any particular "right" way to do it, but a fairly straightforward way would be to change it to this:
function downloadlink ($d_id)
{
$html = #get_url ('' . 'http://www.example.com/' . $d_id . '/go.html');
preg_match('/\<iframe src="(.+?)"/', $html, $matches);
return $matches[1];
}