Jump to content

how to make a variable out of text


optikalefx

Recommended Posts

if the screen displays  <include picture# 3>

how can i store 3 to a variable.  heres the situation:

<?php
$string = 'The <include picture# 3> brown jumped over the lazy dog.';

$con = mysql_connect("xxxx","xxxx","xxxx"); 
mysql_select_db("update1", $con);
$sql = mysql_query("SELECT pic FROM pics WHERE num='$pictureNumber'");
while($row = mysql_fetch_array($sql))
{
$filename = $row['pic'];
}

$patterns[0] = '/<include picture# ([0-9]+)/';
$replacements[2] = '<img src=http://www.4tenproductions.com/ir/uploadedfiles2/' . $filename;
echo preg_replace($patterns, $replacements, $string);


?>

 

 

I need to be able to change that number to anything, and choose its rightful URL from the database.

yes this is a continuation of my other thread, sorry i didnt mean to make 2

 

Link to comment
Share on other sites

im not sure if im just not understanding, or if i just think that wont work.

 

i cant change this line  <include picture# 3>

 

the only way i can change it is if it something reallllllly easy to understand, like  $_pic['3'] or whatever, is too complicated.  when i program, i do it assuming people using my stuff are dumb.

 

this would be ok tho  $pictureNumber=3

 

so many i can somehow parse that variable out of there?

 

Is there a way to use superglobal variables from a line, and get that 3 somehow?

or is there a different approach i have to take?

 

 

Link to comment
Share on other sites

i have a table in a database that stores the URL of a picture and gives it a picture number

 

pic is the column of the url

num is the column of the number

 

the purpose of all this is, iv made a website that brings in the text from another website (both that i own) and you can edit the text, and update it.  bascially making a website updater.  So instead of having to type in all the database info to get the url and such I want to be able to type <include picture# 3>

 

and i dont want to use <img src=""> because all the pictures show up in a neat little table, and i want the text that i see to have as little code as possible.

 

 

(moreover the website is for a client, and this is so they can update it without going through me, the web guy, and this goes around the client having to know any html at all)

 

all i gotta figure how to do at this point is access that 3 so i can use it to select the right picture.

Link to comment
Share on other sites

I'm still extremly confused on your structure becuase if the number is variable why is it part of a flat string to start with? your best idea is somehow haveing a url like mypage.php?imgnum=3

then say

<?php
$image_num = $_GET['imgnum'];
$string = " 'The <include picture# ".$image_num."> brown jumped over the lazy dog.';
?>

if that makes sense

Link to comment
Share on other sites

ok let me see if i can explain myself a little better.

 

this is some textthis is some textthis is some textthis is some textthis is some text

this is some textthis is some textthis is some textthis is some textthis is some text

this is some textthis is some textthis is some textthis is some textthis is some text

<include picture# 15>

this is some textthis is some textthis is some textthis is some textthis is some text

this is some textthis is some textthis is some textthis is some textthis is some text

this is some textthis is some textthis is some textthis is some textthis is some text

 

 

this is what is stored in the database for the clients page

so when you load the clients page, you should see that, but a picture instead of the <include picture# 15>

 

the client is updating his website himself.  He knows he wants picture 15 between those paragraphs. but he doesnt know how to put the picture there.

he also doesnt know any coding.

 

heres a different example im using for the same task

I am having the user type <heading> Text <end heading>

because he knows that Text will be the cool styled heading text that i made.

 

see if i told him to type in <div id='head'> Text </div>  he would get confused, and probably mess it up.

 

so on the clients page i have this code, which gets the text from the database, as well as replaces <heading> with <div id=head> and <end heading> with </div>

<html>
<head>
<style type="text/css">
p {text-align:left;color:#333333;font-family:Times New Roman;font-size:17;}
#head {text-align:left;color:#014342;font-family:Tahoma;font-size:20}
#green {text-align:left;color:#014342;font-family:Times New Roman;font-size:20}
</style>
</head>
<body bgcolor="white">
<?php
$con = mysql_connect("xxxx","xxxx","xxxx"); 
mysql_select_db("update1", $con);
$sql = mysql_query("SELECT value FROM JohnRobberts WHERE fname='clients'");
while($row = mysql_fetch_array($sql))
{
$string =  $row['value'];
}

//create tag heading
$patterns[0] = '/<heading>/';
$patterns[1] = '/<end heading>/';
$replacements[1] = '<span id=head>';
$replacements[0] = '</span>';

echo preg_replace($patterns, $replacements, $string);

?>
</body></html>

 

 

see all the text is inside ['value']  and that is html escaped, so i cant use the global variable, because it wont show up here.

Link to comment
Share on other sites

if i understand correctly, you are trying to get the value of N in:

 

<include picture# N>

 

If you need to get the value of N, you might want to try

 

int preg_match ( string pattern, string subject [, array &matches [, int flags [, int offset]]] )

 

and look in &matches.

 

perhaps like this:

 

$pattern = '/<include picture# ([0-9]+)>/';
$matches = array();
if (preg_match($pattern, $string, $matches)) {
  // found at least one match:
  $image_number = $matches[1];
}

 

Edited: $matches[0] will be the part that matches. $matches[1] is the value in the parentheses. $image_number is N, assuming N is always a number [0-9]+

 

Link to comment
Share on other sites

the only problem i see with that is if i have 30 pictures on one page

 

like

<include picture# 1>

<include picture# 2>

<include picture# 3>

<include picture# 4>

 

if that is storing the number to the first index of the array, then they will all be the same picture.

 

 

Link to comment
Share on other sites

iv been looking through all the sting codes for php and iv come close, but still not good yet.

 

there has got to be one, that searches for <include picture# and then gets the value next

 

substr() go real close but it always started at the beginning of the string.

like this would be perfect

$pictureNumber = substr($string,"<include picture#",1);

but i cant have text in the middle there, it has to be a number starting postion, and i have no idea where this tag will be in the string.

Link to comment
Share on other sites

<?php
imget= $_GET['image']
if ($imget == 1){
include ('images/image1.png'); //Change for where your Image is really located
}
if ($imget == 2{
include ('images/image2.png'); //Change for where your Image is really located
}
if ($imget == 3{
include ('images/image3.png'); //Change for where your Image is really located
}
if ($imget == 4{
include ('images/image4.png'); //Change for where your Image is really located
}
if ($imget == 5{
include ('images/image5.png'); //Change for where your Image is really located
}
if ($imget == 6{
include ('images/image6.png'); //Change for where your Image is really located
}
if ($imget == 7{
include ('images/image7.png'); //Change for where your Image is really located
}
if ($imget == 8{
include ('images/image8.png'); //Change for where your Image is really located
}
if ($imget == 9{
include ('images/image9.png'); //Change for where your Image is really located
}
if ($imget == 10){
include ('images/image10.png'); //Change for where your Image is really located
}
if ($imget == 11){
include ('images/image11.png'); //Change for where your Image is really located
}
if ($imget == 12){
include ('images/image12.png'); //Change for where your Image is really located
}
if ($imget == 13){
include ('images/image13.png'); //Change for where your Image is really located
}
if ($imget == 14){
include ('images/image14.png'); //Change for where your Image is really located
}
if ($imget == 15){
include ('images/image15.png'); //Change for where your Image is really located
}
if ($imget == 16){
include ('images/image16.png'); //Change for where your Image is really located
}
if ($imget == 17){
include ('images/image17.png'); //Change for where your Image is really located
}
if ($imget == 18){
include ('images/image18.png'); //Change for where your Image is really located
}
if ($imget == 19){
include ('images/image19.png'); //Change for where your Image is really located
}
if ($imget == 20){
include ('images/image20.png'); //Change for where your Image is really located
}
if ($imget == 21){
include ('images/image21.png'); //Change for where your Image is really located
}
if ($imget == 22){
include ('images/image22.png'); //Change for where your Image is really located
}
if ($imget == 23){
include ('images/image23.png'); //Change for where your Image is really located
}
if ($imget == 24){
include ('images/image24.png'); //Change for where your Image is really located
}
if ($imget == 25){
include ('images/image25.png'); //Change for where your Image is really located
}
if ($imget == 26){
include ('images/image26.png'); //Change for where your Image is really located
}
if ($imget == 27){
include ('images/image27.png'); //Change for where your Image is really located
}
if ($imget == 28){
include ('images/image28.png'); //Change for where your Image is really located
}
if ($imget == 29){
include ('images/image29.png'); //Change for where your Image is really located
}
if ($imget == 30){
include ('images/image30.png'); //Change for where your Image is really located
}


//Then where you want your pic locat3d type echo $imget;

echo $imget;
?>

 

Hope this helps?

Link to comment
Share on other sites

The best advice I can come up with for this one is to make a separate page..........called image.php

that takes a $_GET parameter for the image ID

 

like yourdomain.com/image.php?id=15

 

then on image.php you can have

$con = mysql_connect("xxxx","xxxx","xxxx"); 
mysql_select_db("update1", $con);
$sql = mysql_query("SELECT pic FROM pics WHERE num=" . $_GET['id']) or die(mysql_error());
$row = mysql_fetch_array($sql));

//Taken from http://us2.php.net/manual/en/function.imagecreatefromjpeg.php
function LoadJpeg($imgname)
{
    $im = @imagecreatefromjpeg($imgname); /* Attempt to open */
    if (!$im) { /* See if it failed */
        $im  = imagecreatetruecolor(150, 30); /* Create a black image */
        $bgc = imagecolorallocate($im, 255, 255, 255);
        $tc  = imagecolorallocate($im, 0, 0, 0);
        imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
        /* Output an errmsg */
        imagestring($im, 1, 5, 5, "Error loading $imgname", $tc);
    }
    return $im;
}
header("Content-Type: image/jpeg");
$img = LoadJpeg("http://www.4tenproductions.com/ir/uploadedfiles2/" . $row['pic']);
imagejpeg($img);
?>

 

 

and then add this to your list of patterns that you have

$patterns[3] = '/$replacements[3] = 'echo preg_replace($patterns, $replacements, $string);

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.